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

In summary, the programmer was trying to create a multiplayer RPG game in Java, but ran into a problem where other variables in the code changed randomly.
  • #1
engri
4
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
  • #2
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?
 
  • #3
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:

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

1. How can I prevent arrays from impacting each other in C with Mingw32?

The best way to prevent arrays from impacting each other is to make sure they are properly declared and initialized separately. This means using different variable names and ensuring they have different memory addresses.

2. Why do arrays impact each other in C with Mingw32?

This can happen if the arrays are declared or initialized improperly, or if they are accidentally sharing the same memory address. It can also occur if the code is not properly structured, leading to unintended changes in the arrays.

3. How can I debug issues with arrays impacting each other in C with Mingw32?

One way to debug this issue is to use a debugger tool, such as GDB, to step through the code and track the changes in the arrays. You can also use print statements to check the values of the arrays at different points in the code.

4. Is there a specific function or method to prevent arrays from impacting each other in C with Mingw32?

No, there is no specific function or method to prevent arrays from impacting each other. It is important to properly declare and initialize the arrays and to keep track of their memory addresses to prevent any unintended changes.

5. Can other variables besides arrays impact each other in C with Mingw32?

Yes, other variables besides arrays can also impact each other if they are not properly declared or initialized. It is important to follow proper coding practices and pay attention to memory addresses to prevent any unintended changes in variables.

Similar threads

  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
6
Views
5K
  • Programming and Computer Science
Replies
6
Views
7K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
7
Views
4K
  • Programming and Computer Science
Replies
2
Views
2K
Back
Top