By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,949 Members | 946 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,949 IT Pros & Developers. It's quick & easy.

dynamic memory in c++

P: 1
While working on dynamic memory in c++ I noticed that values of my previous variables were changing whenever i invoke a loop.

for the following code::
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. using namespace std;
  5.  
  6.  
  7. template <class T>
  8. class Node {
  9. private:
  10.  
  11. public:
  12.     T item;
  13.     Node<T>* next;    
  14.     Node(T a,Node<T>* c) {item = a;next = c;};
  15.     void set_item(T a)  {item = a;};
  16.     void set_item(Node<T>* c) {next = c;};    
  17. };
  18.  
  19.  
  20. template <class T>
  21. class List {
  22. private:
  23.         Node<T>* head;
  24.         Node<T>* tail;
  25.         int length;
  26. public:
  27.         List() {head = NULL;tail = NULL;length=0;};
  28.         int size();
  29.         bool empty();
  30.         void push_back(T);
  31.         void push_front(T);
  32.         void insert(int,T);
  33.         void print();
  34.         bool ismember(T);//pain
  35.  
  36.         // Member functions to
  37.         // interface with list
  38. };
  39.  
  40. template <class T>
  41. void List<T>::push_front(T a){
  42.     length++;                  //initially head->x1->null
  43.     Node<T>b(a,head);          //now head->new entry->x1->null
  44.     head = &b;
  45.     if(length == 1) tail = &b;
  46.     cout<<"look push front gives"<<(*head).item<<endl;
  47.     }
  48.  
  49. template <class T>
  50. void List<T>::print(){
  51.         cout<<"here"<<(*head).item<<endl;
  52.     Node<T> *mynode;
  53.     mynode = head;
  54.     cout<<"here   "<<(*head).item<<endl;
  55.     for(int i = 0;i < length;i++,mynode = (*mynode).next)
  56.     {cout<<i<<"th entry is "<<(*mynode).item<<endl;};
  57. }
  58.  
  59. int main(){
  60. List<int> l;
  61. cout<<l.size()<<endl;
  62. l.push_front(23);
  63. l.print();
  64.  
  65. return 0;}
  66.  
  67.  

the out put after compiling is

look push front gives23
here23
here -1077380900
0th entry is -1077380900

but this works fine if the for loop in print() is quoted.
what is the problem?
Aug 13 '07 #1
Share this Question
Share on Google+
2 Replies


ilikepython
Expert 100+
P: 844
While working on dynamic memory in c++ I noticed that values of my previous variables were changing whenever i invoke a loop.

for the following code::
Expand|Select|Wrap|Line Numbers
  1. template <class T>
  2. class List {
  3. private:
  4.         Node<T>* head;
  5.         Node<T>* tail;
  6.         int length;
  7. public:
  8.         List() {head = NULL;tail = NULL;length=0;};
  9.         int size();
  10.         bool empty();
  11.         void push_back(T);
  12.         void push_front(T);
  13.         void insert(int,T);
  14.         void print();
  15.         bool ismember(T);//pain
  16.  
  17.         // Member functions to
  18.         // interface with list
  19. };
  20.  
  21. template <class T>
  22. void List<T>::push_front(T a){
  23.     length++;                  //initially head->x1->null
  24.     Node<T>b(a,head);          //now head->new entry->x1->null
  25.     head = &b;
  26.     if(length == 1) tail = &b;
  27.     cout<<"look push front gives"<<(*head).item<<endl;
  28.     }
  29.  
  30. template <class T>
  31. void List<T>::print(){
  32.         cout<<"here"<<(*head).item<<endl;
  33.     Node<T> *mynode;
  34.     mynode = head;
  35.     cout<<"here   "<<(*head).item<<endl;
  36.     for(int i = 0;i < length;i++,mynode = (*mynode).next)
  37.     {cout<<i<<"th entry is "<<(*mynode).item<<endl;};
  38. }
  39.  

the out put after compiling is

look push front gives23
here23
here -1077380900
0th entry is -1077380900

but this works fine if the for loop in print() is quoted.
what is the problem?
I'm guessing that has something to do with undefined behaivior because otherwise I can't explain it. I couldn't believe it myself, the first time, but changing your push_front function seems to be working:
Expand|Select|Wrap|Line Numbers
  1. template <class T>
  2. void List<T>::push_front(T a){
  3.     length++;                  //initially head->x1->null
  4.     Node<T> b(a, head);        //now head->new entry->x1->null
  5.     head = &b;
  6.     if(length == 1) tail = &b;
  7.     cout<<"look push front gives"<<(*head).item<<endl;
  8.     }
You create the b node as an object and then assign it to a pointer. Try declaring it like this:
Expand|Select|Wrap|Line Numbers
  1. Node<T> *b = new Node<T> (a, head);
  2.  
And then assign to head like this:
Expand|Select|Wrap|Line Numbers
  1. head = b;
  2.  
Try it and see if it works.

PS In your node classs you have item and next declared as a public variable and you have accesor functions. That's pointless. Either make them private or you don't need setitem.
Aug 13 '07 #2

ilikepython
Expert 100+
P: 844
Also, I think the problem with assigning the pointer to an adress of an object is that that object is local so is deleted when the function exits. Then you have a pointer that points to garbage (nothing).
Aug 14 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.