TIP: Click on subject to list as thread! ANSI
echo: cis.os9.68000.osk
to: all
from: Bob van der Poel 76510,2203
date: 1991-06-24 20:58:53
subject: #Allocation help

#: 11185 S12/OS9/68000 (OSK)
    24-Jun-91  20:58:53
Sb: #Allocation help
Fm: Bob van der Poel 76510,2203
To: all

I'm wondering if anyone has a suggestion for me. I'm using a singly linked list
to store strings of an unknown length. We I started I set up the following
structure:

 struct data{
   struct data *next;
   char *text;
 }

Then I'd malloc() memory for the structure and for the text storage. Of course,
the two memory requests are not necessary, so I'm now getting enough memory for
the 'next' pointer plus the text. The question is: is there an elegant way to
ensure portablility and make this a bit more readable later. For some reason,
the following leaves a great deal to be desired:

 woof=malloc(sizeof(struct foo)+strlen(buffer));
   ...check for valid p & setup links
 strcpy(woof+1,buffer);

I tried setting up a dummy pointer in the structure, but that doesn't seem to
work. The other solution is to have 'text' in the structure defined as 'char
text[1]' and then do the malloc() for sizeof(struct data)-1+strlen(buffer). One
of the problems with this is that we have to assume the order of the data in
the structure, and I don't believe that element 1 in a structure HAS to follow
element 2 (even though it does in most implementations). Guess the safest is to
do 2 malloc()s.

Any ideas?

There is 1 Reply.

SOURCE: compuserve via textfiles.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.