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

  • Thread starter Thread starter engri
  • Start date Start date
  • Tags Tags
    Arrays Impact
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 3K views
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:
Physics 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: