Re: Linked List & Dynamic Memory Allocation
- From: "Nobody" <Nobody@xxxxxxxxx>
- Date: Mon, 27 Aug 2007 16:33:00 -0700
Hi,
Think of stack being defined at compile time.
Think of heap being undefined at compile time and defined at run-time.
SomeFunc()
{
char arrayStack[100]; <- Compiler knows. Stack
char* arrayHeap = new char[Some_Calculated_Value]; <- Compiler does not know. Heap.
delete [] arrayStack; /* Error. It is automatically deleted */
delete [] arrayHeap; /* Correct . You must take care of deleting objects created with new or malloc. */
}
HTH,
"one-trick-pony" <worldofpain.aamir@xxxxxxxxx> wrote in message news:1188253920.562326.312270@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Interesting point. When I code in C/C++ I choose not to think about.
stack and heap. I focus on code than worry about the deep down
details of what is happening. I would like to learn that sometime but
my current goal is to learn the language and manipulating data
structures etc. Both of you mentioned stack and heap in this
example. I am not sure where that fits into the picture. I know, for
instance, when I call malloc it uses heap to allocate memory. But I
don't care where and how it gets memory as long as I get memory
allocated for my program. Surely, I would like to learn these
concepts. I am interested if you can shed light into this.
Like I mentioned, this is an excercise, I have a very simple program.
There is only main routine-no functions. However, it seems I have to
be careful when I call other functions and manipulate linked list
because then stack becomes involved. I am not sure how will that
effect my program.
The last thing I tried was the first reponse from Jonathan Wood in an
attempt to append items to a linked list.
m_nMode[MAX-1].next = pNewRec;
pNewRec->next = NULL;
It worked by allowing me to attach only 1 item. However, I want the
ability to keep on attaching items until user chooses to quit. I have
a loop that asks user after every entry whether they want to continue
or not. Inside that loop I have this line of code:
nodes* pNewRec = (nodes *) malloc(sizeof(nodes));
My struggle is achieving attachment of new records into existing array
of structures. That is array, nodes m_node[MAX];
should expand to accomadate new records. I hope you understand what
I am trying to do. Thanks for tips. I am learning. Again, please
tell me about stack and heap and how it can effect my program.
Thanks.
- Follow-Ups:
- Re: Linked List & Dynamic Memory Allocation
- From: Joseph M . Newcomer
- Re: Linked List & Dynamic Memory Allocation
- References:
- Linked List & Dynamic Memory Allocation
- From: one-trick-pony
- Re: Linked List & Dynamic Memory Allocation
- From: Jonathan Wood
- Re: Linked List & Dynamic Memory Allocation
- From: one-trick-pony
- Re: Linked List & Dynamic Memory Allocation
- From: Dan Bloomquist
- Re: Linked List & Dynamic Memory Allocation
- From: one-trick-pony
- Re: Linked List & Dynamic Memory Allocation
- From: Dan Bloomquist
- Re: Linked List & Dynamic Memory Allocation
- From: one-trick-pony
- Linked List & Dynamic Memory Allocation
- Prev by Date: Re: Linked List & Dynamic Memory Allocation
- Next by Date: Re: Linked List & Dynamic Memory Allocation
- Previous by thread: Re: Linked List & Dynamic Memory Allocation
- Next by thread: Re: Linked List & Dynamic Memory Allocation
- Index(es):
Relevant Pages
|