Thread: Problem with malloc

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    63

    Red face Problem with malloc

    First I allocate a memory area like this:

    // allocate 1024 bytes for the record buffer //
    if((map[PartIndex] = malloc(sizeof(**map) * 1024)) == NULL)
    {
    printf("malloc map[PartIndex] failed\n");
    }

    Then I copy the pointer (I don't want to touch the original pointer) to the area like this:

    myPtr = map[PartIndex];

    PartIndex is 0 from the beginning, and the second time I allocate it's 1 but...

    myPtr saves bytes to the first memory area every time, I can't understand why.
    You cantīt teach an old dog new tricks.

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    63

    Thumbs up Re: Problem with malloc

    Originally posted by electrolove
    First I allocate a memory area like this:

    // allocate 1024 bytes for the record buffer //
    if((map[PartIndex] = malloc(sizeof(**map) * 1024)) == NULL)
    {
    printf("malloc map[PartIndex] failed\n");
    }

    Then I copy the pointer (I don't want to touch the original pointer) to the area like this:

    myPtr = map[PartIndex];

    PartIndex is 0 from the beginning, and the second time I allocate it's 1 but...

    myPtr saves bytes to the first memory area every time, I can't understand why.
    Strange, it seems to work now, hmmm...
    You cantīt teach an old dog new tricks.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Need to see more code....
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    63
    Originally posted by Hammer
    Need to see more code....
    Thanks for your answer but as I already told it works now
    You cantīt teach an old dog new tricks.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by electrolove
    Thanks for your answer but as I already told it works now
    If it magically started working, it might still be broken Oh well, post again if and when it breaks
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    63
    Originally posted by Hammer
    If it magically started working, it might still be broken Oh well, post again if and when it breaks
    I just think it was an human error
    You cantīt teach an old dog new tricks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Replies: 12
    Last Post: 06-24-2005, 04:27 PM
  3. malloc and realloc
    By odysseus.lost in forum C Programming
    Replies: 3
    Last Post: 05-27-2005, 08:44 AM
  4. freeing problem with concurrent processes
    By alavardi in forum C Programming
    Replies: 2
    Last Post: 03-07-2005, 01:09 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM