#Structure woes
9 messages in this thread
Question all,
Here's the situation, I need to be able to have a linked list of structures
in memory, although I don't want to allocate memory that might not be used and
I don't want to limit myself in that fasion. Here is an example structure:
struct _BUFFER {
struct _BUFFER *Next;
struct _BUFFER *Prev;
char Name; };
#typedef struct _BUFFER Buffer
Ok, I now have a single buffer allocated. Now what I'm thinking is that I
would do an AllocMem() with the sizeof(Buffer) to allocate another area to use
and then set the pointers to link them up. Am I right in my frame of thinking?
or would there be something else that I might not've considered? Any help can
be sent to one of the following or a reply left here…Thankx!!
+—————————–+——————————-+ |Deryk Robosson
|Only __ A1000 14Mhz 1 Meg | |'Write your own damn code!' | Amiga ///
Networked to | | | Makes /// another A1000.
| |jester@m-net.ann-arbor.mi.us | _It /// 1.3,USRobotics 9600,|
|drobosson@mastro.uucp | \\\ /// Supra 2400 |
|76124.1426@compuserve.com | \\X//Possible! |
+—————————–+——————————-+
That's what I do, malloc and link. If it's a linear linked list instead of a
circular one, make *sure* to set the pointers at each end to NULL and to
*check* for NULL *every* time you move along the list. If I emphasized each
word in this message it still wouldn't equal the number of times I've crashed
my machines by not doing that and by making assumptions.
Heres a line or two of code:
if ( !(bufptr = (Buffer *) malloc( sizeof( Buffer ) )) )
{ printf( "Memory problem\n" ); /* error handling here */ }
Next week, we'll do recursion.
Mike McCormick
I would use the exec list handling functions wherever possible, just make the
first part of your structure look like or actually contain a MinNode struct.
Look in exec/lists.h or nodes.h. Using these functions takes care of alot of
the list linking, management and bookkeeping, which is both easier, and less
susceptible to silly errors.
Whelp, you JUST went over my head as far as the OS goes…I'm not all that
familiar with that area of the OS. For me, I'm on the 'use what I know' level
and attempt what I don't know later type thing.. 🙂 Perhaps you could fill us
in a little?
Deryk
Arnie,
Using the exec linked list stuff is much more memory inefficient and much
more error prone (due to unnecessary complexity) than doing it "by hand".
Linked list creation and manipulation is trivial — unless you use the
exec stuff.
-sja
Yes, what you've described is one way of doing it.
Can you do be a favor? We have a full name policy in the AmigaForums, so
we can tell who is who. Please use the options command to change your
name.
Also, generally we just use one line signatures here instead of multiple
line signatures like they do on USENET. Can you change that too?
Thanks!
Steve
Sorry….I can't remember why my handle got mixed up, but it did. I like not
to use the signature when replying, but when posting, I like to be able to get
replies via other methods. Deryk
Hi there,
As others have mentioned, I would use the exec list functions. Why bother to
write and debug code when you can use someone elses? Failing that, there was a
very nice, efficient set of list handling routines published in an old copy of
Amazing Computing. I you wish, I could email you the source code, and some
examples on how they work.
Steve the G. [BEDFORDSHIRE, UK]
Steve,
I would really appreciate the source code that you could email me. I feel
that it would help out tremendously. I'll be looking for it. Thanks!
Deryk