Re: Linked List & Dynamic Memory Allocation



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.


.



Relevant Pages

  • Re: Linked List & Dynamic Memory Allocation
    ... Both of you mentioned stack and heap in this ... when I call malloc it uses heap to allocate memory. ... It worked by allowing me to attach only 1 item. ...
    (microsoft.public.vc.mfc)
  • Re: How does managed code work?
    ... Does it work the same way as the native stack with a frame pointer that is the head of a linked list of stack frames where each time we enter a function we create a new stack frame in which new variables are pushed and each time we exit a function the entire stack frame is popped? ... Can someone point me to a discussion of the managed heap? ... How does it prevent memory leaks that occur in COM when two objects reference each other and keep the others reference count nonzero? ... Because objects don't go out of scope, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Groovy (war Re: [PROST]Re: CDC Plugin fuer Eclipse)
    ... Warum müsste man continuations erlauben Stackvariablen in den Heap zu verschieben? ... Ein activation record speichert die Parameter und lokalen Variablen der Funktion und eine Information, ... Da diese activation records klassischerweise auf einem Stack verwaltet werden, kann man hier Stackspace sparen - Endrekursion ist damit genauso effizient wie eine klassische Iteration mit einem Sprung. ...
    (de.comp.lang.java)
  • Re: Please Explain where will the struct be stored if it is declared inside the Class
    ... forget about structs for a second. ... can be stored either on the stack, or on the heap. ... First, think about the stack. ... A struct would act exactly the same as any of these decimals and ints. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Is MSDN wrong? or I made a mistake? about static member function
    ... the heap" or some such reference. ... shouldn't we have a uniform notation? ... You can tell a heap object from a stack object by ... the result of trying to build a compiler on a tiny computer by someone who wasn't a very ...
    (microsoft.public.vc.mfc)