Dan Senior - 2011-09-07

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>

using namespace std;

struct testStruct{
     int a;

     testStruct *Next;
     };

class linkedList
{
     public:
          testStruct *Head, *curRec;

     //constructor
     linkedList::linkedList()
     {
          size=0;
          Head=NULL;
     }

     void linkedList::Add(testStruct *newItem)
     {
          testStruct *newRec=new testStruct;

          newRec=newItem;
          newRec->Next=Head;
          Head=newRec;
          size++;
     };
};

int main(int argc, char *argv[])
{
     linkedList *list=new linkedList();

     testStruct *rec;

     //Add some records

     for (int cInt=0; cInt<10; cInt++)
     {
          rec->a=cInt;
          list->Add(rec);
     }

     return 0;
}

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?