Thread: Memory management

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    15

    Memory management

    So I'm trying to figure out if I'm dealing with memory correctly by printing out pointer values...I can't find gdb for windows...yes, tried mingw but there was no gdb.exe in it.

    Code:
    fprintf(stderr, "Allocating %d bytes for %d items, Table.\n", (sizeof(PTR)*storageSize), storageSize);
     tbl->Items = (struct TableItem**)malloc(sizeof(PTR)*storageSize);
      fprintf(stderr, "Items: %p to %p\n", tbl->Items, tbl->Items + (sizeof(PTR)*storageSize));
      for (count=0; count<storageSize; count++)
      {
        tbl->Items[count] = NULL;
        fprintf(stderr, "Item %d, byte %p to %p\n", count, &tbl->Items[count], (&tbl->Items[count])+(sizeof(PTR)));
    I'm defining PTR as a long.

    This is the output I'm getting...

    Allocating 8 bytes. struct Table.
    Allocating 40 bytes for 10 items, Table.
    Items: 003D1138 to 003D11D8
    Item 0, byte 003D1138 to 003D1148
    Item 1, byte 003D113C to 003D114C
    Item 2, byte 003D1140 to 003D1150
    Item 3, byte 003D1144 to 003D1154
    Item 4, byte 003D1148 to 003D1158
    Item 5, byte 003D114C to 003D115C
    Item 6, byte 003D1150 to 003D1160
    Item 7, byte 003D1154 to 003D1164
    Item 8, byte 003D1158 to 003D1168
    Item 9, byte 003D115C to 003D116C

    I expected the last memory location to be the same as the last location allocated for my array's space but it was not. I also expected the end of Item 0 to be the beginning of Item 1, but that wasn't the case either. I checked the sizeof(long) vs sizeof(struct TableItem*) and they were the same.
    What am I doing wrong???

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Take note that
    Code:
    int *p;
    p = malloc( sizeof(int [10]) );
    
    p = p + 1;  // point to next int ! not next byte!
    equalivent to
    p = (char*)p + sizeof(*p);   // OK ??

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    15
    Yeah but isn't that what I have?

    Code:
    p[0]+sizeof(int) == p[1] // true?
    
    // so shouldn't this be true?
    tbl = (Item**)malloc(sizeof(long)*10);
    /*
    That should be the same as
    tbl = (Item*)malloc(sizeof(long[10]));
    */
    tbl[0]+sizeof(long) == tbl[1] // not true when i print it

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    int *p;
    //...
    p = p + 1;

    makes p point to next integer.

    if you have p = p + sizeof(int);
    assuming sizeof(int) is 2;
    p = p + 2; // p becomes &p[2]

    makes p point to next next integer.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    15
    Oh ok. That solved it. Thanks Bayint.

  6. #6
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    About gdb, have you tried cygwin, Minimalist GNU Windows(MinGW) is exactly what it's name says. I've heard Cygwin os more complete, but idk.

    If Cygwin leaves you hanging, OllyDbg is a great debugger. I wish there was a Linux port, it's that good.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory management woes
    By homer_3 in forum C Programming
    Replies: 3
    Last Post: 02-16-2010, 02:29 PM
  2. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 11:41 AM
  3. Replies: 7
    Last Post: 02-06-2009, 12:27 PM
  4. Valgrind says I suck at memory management :)
    By carrotcake1029 in forum C Programming
    Replies: 6
    Last Post: 02-01-2009, 08:10 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM