An error in allocation

We have an issue with this code


struct foo {
 int NumberOfPointers;

 char *Pointers[1];
} xx;

// Allocate A Block Of Pool With Room For 100 Pointers

myHunkyStuff = malloc( sizeof(long) + 100 * sizeof (char *) );

Can anyone explain?

The H Matherson Crew

Parents
  • What is the problem, exactly? To get good answers you need to ask good questions, and you haven't even specified if this is a compiler problem (i.e does the compiler give you errors?) or a runtime problem (i.e, the code doesn't do what you want).

    That said, your code doesn't make much sense. I'm guessing that you want to have one copy of the struct, and then allocate memory for 100 pointers to char, which is then to be assigned to the member Pointers of said struct. Instead, you're allocating memory for 100 pointers to char and one long.

    Please describe what it is you want to achieve, and then you might get some sensible help.

    Regards
    -Øyvind

Reply
  • What is the problem, exactly? To get good answers you need to ask good questions, and you haven't even specified if this is a compiler problem (i.e does the compiler give you errors?) or a runtime problem (i.e, the code doesn't do what you want).

    That said, your code doesn't make much sense. I'm guessing that you want to have one copy of the struct, and then allocate memory for 100 pointers to char, which is then to be assigned to the member Pointers of said struct. Instead, you're allocating memory for 100 pointers to char and one long.

    Please describe what it is you want to achieve, and then you might get some sensible help.

    Regards
    -Øyvind

Children
More questions in this forum