Thread: Strange malloc

  1. #1
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501

    Strange malloc

    I am allocating this 2d array and well, let me post the code first:
    Code:
    struct cell {
            int oldPos, newPos;
    };
    int main(void) {
            int x, y;
            struct cell **world;
    
            world = malloc(LINES * sizeof (struct cell*));
            for(x = 0;x < COLS; x++) {
                    world[x] = malloc(COLS * sizeof (struct cell));
            }
            for(x = 0; x < COLS; x++) {
                    for(y = 0; y < LINES; y++) {
                            world[x][y].newPos = 0;
                    }
            }
            return 0;
    }
    I know that I am not checking return values or all of that good stuff, and LINES and COLS are defined in a header as say 80 and 24 respectively. When I run the program, it segfaults when x reaches 27 in the assignment loop, but using a debugger, I found world[28][0] was valid. It appears that every other element past 27 is valid? Here is the gdb output from some commands.
    Quote Originally Posted by gdb
    (gdb) print world[27][0]
    Cannot access memory at address 0x0
    (gdb) print world[28][0]
    $3 = {oldPos = 0, newPos = 0}
    (gdb) print world[29][0]
    Cannot access memory at address 0x0
    (gdb) print world[30][0]
    $4 = {oldPos = 0, newPos = 0}
    (gdb) print world[31][0]
    Cannot access memory at address 0x0
    (gdb) print world[32][0]
    $5 = {oldPos = 0, newPos = 0}
    (gdb) print world[33][0]
    Cannot access memory at address 0x0
    (gdb) print world[34][0]
    $6 = {oldPos = 0, newPos = 0}
    (gdb) print world[35][0]
    Cannot access memory at address 0x0
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You've got your constants (LINES and COLS) all mixed up. Try replacing the constants temporarily with their values to better see the mix up.

  3. #3
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Thanks, I got it because of you, I should have had this for the mallocing
    Code:
    */.../*
    world = malloc(LINES * sizeof (struct cell*));
            for(x = 0;x < LINES; x++) {
                    world[x] = malloc(COLS * sizeof (struct cell));
            }
    /*...*/
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You also need to swaps LINES and COLS here.
    Code:
            for(x = 0; x < COLS; x++) {
                    for(y = 0; y < LINES; y++) {
                            world[x][y].newPos = 0;
                    }
            }

  5. #5
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Yeah, I fixed it there too, but I didn't post it. I just wanted the memory to be finally allocated right. Thanks again.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple but strange code of malloc()
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 03-31-2008, 04:58 AM
  2. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 12:26 PM
  3. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  4. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 09:08 AM