[Problem] C, Mingw32 - arrays impact each other (changing values)

  • Thread starter Thread starter engri
  • Start date Start date
  • Tags Tags
    Arrays Impact
AI Thread Summary
A user is developing a multiplayer RPG game using J2ME for the client and C with MinGW32 for the server. They initially anticipated challenges with network programming but successfully established multiple connections. However, they encountered a critical issue where changes to one array, `bytes_sent`, inadvertently affect another array, `player_y`, due to an out-of-bounds access. The user mistakenly declared `bytes_sent` with a size of 16 instead of the intended 65536, leading to memory overlap. This bug can cause unpredictable behavior, as the compiler places arrays in contiguous memory. The user expresses frustration over working alone after a co-programmer's departure and discusses their preference for C over Java due to familiarity and portability concerns. They also mention that J2ME is still relevant, contrary to their expectations of its decline. The user highlights that Java would have provided helpful error messages for out-of-bounds errors, which would have aided in debugging.
engri
Messages
4
Reaction score
0
About week ago I decided to make an attept to create multiplayer rpg game in j2me. The game needs server so I use c and mingw32 as compiler. I thought the biggest problem will be network programming as I never tried that before. But the j2me client is connecting nicely I have managed to get multiple connections (sending/recieving data) from the clients to the server. Now i encoutered very strange problem of other kind :|. Maybe i'll paste some declarations:

[...]
const int D_SOCKETS = 16;
[...]
int main(int argc, char **argv) {
[...]
int player_y[65536];
int bytes_sent[D_SOCKETS];

> then all of the uses of that variables in the file:

player_y[sockets[index]]=rip_y(buffer);
tymczasowa = ((player_y[sockets])/128);
buffer2[6]= 128+tymczasowa;
buffer2[7]= 128+(player_y[sockets])-tymczasowa*128;

for (i=0;i<65536;i++) bytes_sent=0;
bytes_sent[(sockets[index])]++;
for (j=0;j<sockets_index;j++) {
if(sockets[j]!=sockets[index]) {
[...]
bytes_sent[(sockets[j])] += 22;
} }
bytes_sent[(sockets[index])]=0;

so as can you see this values are non related in any way. But each time
bytes_sent[(sockets[j])] += 22;
is executed value of player_y[(sockets[2])]) changes the same way...
exacly bytes_sent[728] = socket_y[712]...
Eeah time I change one the other changes to the same value..

It has some really big arrays so i had to change the stack size to 30mb. I compile it with command:
g++ -Wl,--stack=31457280 sock.c -o main.exe -lwsock32

I use libraries:
#include <stdio.h>
#include <time.h>
#include <Winsock2.h>
#else
#define closesocket close
#endifI know its only one case if I change some minor things it will disappear but I am sure there is more of these mixups. Its just one that I run into. Please help. I have no idea what to do to fix this permanently. I can email the whole code if it is necessary.

screenshots from the engine:
http://www.speedyshare.com/735958536.html
 
Last edited:
Technology news on Phys.org
engri said:
exacly bytes_sent[728] = socket_y[712]...
There's your problem: the index 728 is way out of bounds for the array bytes_sent.

(BTW, you didn't show us the declaration of socket_y)

This is a rather serious bug, and can lead to all sorts of nasty problems. The behavior you're seeing is because the compiler decided to place the socket_y array in memory immediately following the bytes_sent array.

You got kind of lucky that bytes_sent[728] is somewhere innocuously placed in memory.


By the way, why not write the server in Java?
 
DAMN! How could I miss that.. I've been programming all week, seems like I would use a little break. I thought it was declared 65k not 16 :|, and for some time that it was declared 16 but the maximum value of the value is connection index (0-15) not the socket index (up to 2^16). Thats why its nice to have someone that works with you, but it seems that my co-programmer gave up yesterday so I'm on my own. Thanks.
Why not java? I've been using c and gcc for quite some time. (from linux/making simple games on gameboy advance) I have unused computer that has linux installed. If I do it with the help of mingw it will be easily portable. And I can set up the game server there. I don't know java environment at all. The only reason I'm using java that I can't use C. Mophun platform is dead and Symbian is only on Nokia devices. And j2me is literally everywhere. I was supprised to run this engine on old Siemens S65. I was kinda waiting for few years for java to die, but it didn't happen. J2me is getting stronger every year, so i decided to learn a bit.
Here is my firs java game. It's a J2ME version of an old Atari XE game caller Robbo.
http://ftp.pigwa.net/stuff/other/robbojava/
Btw java would report error "out of bounds" and that would be really helpful :)
 
Last edited by a moderator:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top