I am trying to implement a linked list in dev-c++, and all the code seems to
be good, but whenever I call the portion to add a node, it allocates the same
node. I have verified by outputting the address, and every time it is the same
address. Is there some condition that makes the "new" keyword allocate the
same address even when it is called multiple times? Could it be because it is
part of a class?
Here is a snippet of the code:
#include<cstdlib>#include<iostream>usingnamespacestd;structtestStruct{inta;testStruct*Next;};classlinkedList{public:testStruct*Head,*curRec;//constructorlinkedList::linkedList(){size=0;Head=NULL;}voidlinkedList::Add(testStruct*newItem){testStruct*newRec=newtestStruct;newRec=newItem;newRec->Next=Head;Head=newRec;size++;};};intmain(intargc,char*argv[]){linkedList*list=newlinkedList();testStruct*rec;//Add some recordsfor(intcInt=0;cInt<10;cInt++){rec->a=cInt;list->Add(rec);}return0;}
In debugging, I have isolated all the way down to the allocation statement
"testStruct *newRec=new testStruct;"
. When I put a
"cout << &newRec << endl;"
, it shows the same address on every call.
What can I do to make it actually allocate new memory each time?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I am trying to implement a linked list in dev-c++, and all the code seems to
be good, but whenever I call the portion to add a node, it allocates the same
node. I have verified by outputting the address, and every time it is the same
address. Is there some condition that makes the "new" keyword allocate the
same address even when it is called multiple times? Could it be because it is
part of a class?
Here is a snippet of the code:
In debugging, I have isolated all the way down to the allocation statement
. When I put a
, it shows the same address on every call.
What can I do to make it actually allocate new memory each time?