Thread: structs created in same address?

  1. #1
    Registered User nepper271's Avatar
    Join Date
    Jan 2008
    Location
    Brazil
    Posts
    50

    structs created in same address?

    Hello,

    I have the following simple program

    Code:
    #include <stdio.h>
    #include <math.h>
    #define maxG 500
    
    typedef struct _test {
        float c[maxG][maxG][maxG];
        int gX, gY, gZ;
    } test;
    
    int main() {
        int i, j, k;
        test p1, p2;
    
        p1.gX = 5;
        p1.gY = 3;
        p1.gZ = 6;
        p2.gX = 2;
        p2.gY = 6;
        p2.gZ = 3;
    
        return 0;
    }
    which compiles, but gives a segmentation fault on execution.

    I'm on Linux, I compiled using gcc or gcc-4.4, and used gdb to debug and obtained the following

    Code:
    Program received signal SIGSEGV, Segmentation fault.
    main () at main.c:17
    17	    p2.gX = 2;
    (gdb) print p1
    Não é possível acessar a memória no endereço 0x7fffffffe228
    (gdb) print &p1
    $1 = (test *) 0x7fffffffe228
    (gdb) print &p2
    $2 = (test *) 0x7fffffffe228
    (gdb) print &p1.c
    $3 = (float (*)[500][500][500]) 0x7fffffffe228
    (gdb) print &p2.c
    $4 = (float (*)[500][500][500]) 0x7fffffffe228
    Now, am I seeing this wrong, or p1 and p2 are occupying the same space? Anyone can give a hint on what I did wrong?

    Thanks a lot,
    Abel

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Your struct contains a 500x500x500 array. On a system with 4-byte floats, that would be 500000000 bytes. That's close a gigabyte (when you take into account the fact that you're trying to create two of those structs), which is almost certainly too large for your stack, which is where your local variables are going.

    You could make the structs global (or static), but that'll still take up a gig of RAM. You probably want to restructure your program.

  3. #3
    Registered User nepper271's Avatar
    Join Date
    Jan 2008
    Location
    Brazil
    Posts
    50
    Thank you cas, you are right.

    I have changed maxG to 50 and it worked properly.

    Thanks a lot for the reply,
    Abel

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linux raw socket programming
    By cnb in forum Networking/Device Communication
    Replies: 17
    Last Post: 11-08-2010, 08:56 AM
  2. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  3. I thought pointers were pointers...
    By keira in forum C Programming
    Replies: 19
    Last Post: 08-15-2007, 11:48 PM
  4. Block address from word address
    By xddxogm3 in forum Tech Board
    Replies: 0
    Last Post: 04-25-2007, 09:02 PM
  5. MSN Vital Information
    By iain in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 09-22-2001, 08:55 PM

Tags for this Thread