delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/01/23/20:16:57

From: punjab AT ix DOT netcom DOT com (//)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: How to use malloc?
Date: 23 Jan 1998 22:26:30 GMT
Organization: Netcom
Lines: 41
Message-ID: <6ab5em$imm@sjx-ixn6.ix.netcom.com>
References: <34C89EDF DOT ACBAE798 AT no-spam DOT mbnet DOT fi>
NNTP-Posting-Host: stk-ca1-22.ix.netcom.com
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019