www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/01/23/15:40:58

Date: Fri, 23 Jan 1998 12:40:13 -0800 (PST)
Message-Id: <199801232040.MAA27032@adit.ap.net>
Mime-Version: 1.0
To: Jukka Mickelsson <jukka AT mbnet DOT fi>, djgpp AT delorie DOT com
From: Nate Eldredge <eldredge AT ap DOT net>
Subject: Re: How to use malloc?

At 03:45  1/23/1998 +0200, Jukka Mickelsson wrote:
>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
No parenthesis here. Just `struct menuitem { ... }'
>  char *description[25];
>}
Add a semicolon.
>
>struct menuitem menu[5];
>
>for(i=0; i<5; i++)
>for(e=0; e<X; e++)
>menu[i].description[e] = "testing";
Okay, 
>
>The problem is that when X > 3 the program starts to act in a weird way
>or crashes
>sometimes. 
This should work, but note that each string "testing" will be read-only.
Actually, they will all be the same string. If you want a copy in each
place, do something like
menu[i].description[e] = strdup("testing");

>I probably should use malloc() here but I haven't got a
>slightest idea _where_
>and _how_.
I don't see anywhere that needs mallocing.

This is a standard C question and would be far better asked on comp.lang.c.

Nate Eldredge
eldredge AT ap DOT net



- Raw text -


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