Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Jun 5, 2009 #1
    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
    #endif


    I know its only one case if I change some minor things it will dissapear but im 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: Jun 5, 2009
  2. jcsd
  3. Jun 5, 2009 #2

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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?
     
  4. Jun 5, 2009 #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 enviroment 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/ [Broken]
    Btw java would report error "out of bounds" and that would be really helpful :)
     
    Last edited by a moderator: May 4, 2017
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: [Problem] C, Mingw32 - arrays impact each other (changing values)
  1. A problem with C (Replies: 4)

Loading...