Mail Archives: djgpp/1998/01/23/20:16:57
In <34C89EDF DOT ACBAE798 AT no-spam DOT mbnet DOT fi> Jukka Mickelsson <jukka AT mbnet DOT fi>
writes:
>
>I have a problem using the malloc() function since I don't really know
>what to do with it.
>I have the following type program:
>
>struct menuitem() { // this source code is simplified
> char *description[25];
>}
>
>struct menuitem menu[5];
>
>for(i=0; i<5; i++)
>for(e=0; e<X; e++)
>menu[i].description[e] = "testing";
>
Try changing the "description" to this:
char** description;
Then try this before the assignment:
menu[i].(description+e)=(char*)malloc(sizeof(char[strlen("testing"+1)]));
>The problem is that when X > 3 the program starts to act in a weird
way
>or crashes
>sometimes. I probably should use malloc() here but I haven't got a
>slightest idea _where_
>and _how_.
>Could anyone help?
>
See if that helps. The solution I've provided (to be verified by you)
requires the least amount of modification to your existing code and
maximizes code re-use. Quite frankly your code is very bizzare so
engineering considerations were not analyzed; I'd scrap the code and do
the whole thing differently.
- Raw text -