Thread: Why are these pointers suddenly changed?

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    78

    Why are these pointers suddenly changed?

    I created a series of linked lists and then printed them. Notice that I touch new->prev one time, and I verify it has been set via printf at the end of the for loop. However, when I then print all the nodes via printnode(), suddenly the ->prev pointers have all been changed to "0x11". What does that mean? How could this happen?? Where did the pointers go?

    Code:
    			A_previous=NULL; 
    
    			for (i=0; i<n_areas; i++){
    				new=(Area*)malloc(sizeof(Area*)); 
    				if(i==0){head=new;} 
    				new->name=i; 
    				new->n_exits=0; 
    				new->next=NULL; 
    				new->prev=A_previous; 
    				if(A_previous!=NULL){A_previous->next=new;} 
    				A_previous=new; 
    				printf("[[%p]]", new->prev);}
    
    			printnode(head);
    Code:
    void printnode(Area *current) {
        while (current) {
            printf("\n<%d||[%p]>: <%p|%p>",
                   current->name, current, current->prev, current->next);
            current = current->next;
        }
    }

    Output:

    Code:
    Reading: 10 areas...[[(nil)]][[0x804a130]][[0x806b008]][[0x806b018]][[0x806b028]][[0x806b038]][[0x806b048]][[0x806b058]][[0x806b068]][[0x806b078]]
    <0||[0x804a130]>: <(nil)|0x806b008>
    <1||[0x806b008]>: <0x11|0x806b018>
    <2||[0x806b018]>: <0x11|0x806b028>
    <3||[0x806b028]>: <0x11|0x806b038>
    <4||[0x806b038]>: <0x11|0x806b048>
    <5||[0x806b048]>: <0x11|0x806b058>
    <6||[0x806b058]>: <0x11|0x806b068>
    <7||[0x806b068]>: <0x11|0x806b078>
    <8||[0x806b078]>: <0x11|0x806b088>
    <9||[0x806b088]>: <0x806b078|(nil)>
    -Adam Rinkleff
    Last edited by Adam Rinkleff; 07-28-2011 at 11:42 PM.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    I added the following line to the end of the for loop

    Code:
    if(i>1){printf("[[::%p::]]", new->prev->prev);}
    This shows that the pointer value is switching to ox11 at the end of each pass. But why?

    Output
    Code:
    Reading: 10 areas...[[(nil)]][[0x804a158]][[0x806b008]][[::0x11::]][[0x806b018]][[::0x11::]][[0x806b028]][[::0x11::]][[0x806b038]][[::0x11::]][[0x806b048]][[::0x11::]][[0x806b058]][[::0x11::]][[0x806b068]][[::0x11::]][[0x806b078]][[::0x11::]]
    <0||[0x804a158]>: <(nil)|0x806b008>
    <1||[0x806b008]>: <0x11|0x806b018>
    <2||[0x806b018]>: <0x11|0x806b028>
    <3||[0x806b028]>: <0x11|0x806b038>
    <4||[0x806b038]>: <0x11|0x806b048>
    <5||[0x806b048]>: <0x11|0x806b058>
    <6||[0x806b058]>: <0x11|0x806b068>
    <7||[0x806b068]>: <0x11|0x806b078>
    <8||[0x806b078]>: <0x11|0x806b088>
    <9||[0x806b088]>: <0x806b078|(nil)>

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is wrong:
    Code:
    new=(Area*)malloc(sizeof(Area*));
    It should be:
    Code:
    new = (Area*)malloc(sizeof(Area));
    Though I recommend:
    Code:
    new = malloc(sizeof(*new));
    You may or may not find that this magically fixes your problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    Wow! They both work, hm. I'm not even sure why new = malloc(sizeof(*new)); would work, how does it know what the size of new is supposed to be, if it doesn't know its an area*? I guess I don't really know what I'm doing. Oh... I guess it knows the size of what new points to, because I declared new already as an Area*.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Adam Rinkleff
    I guess it knows the size of what new points to, because I declared new already as an Area*.
    Yes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    Laserlight is the best programmer in the whole world!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 21
    Last Post: 06-24-2011, 02:57 AM
  2. Replies: 5
    Last Post: 07-28-2009, 11:37 AM
  3. Still get error even if i changed a lot..
    By icefire99 in forum C++ Programming
    Replies: 9
    Last Post: 04-26-2007, 03:31 AM
  4. Value of structure can't be changed
    By kolistivra in forum C Programming
    Replies: 1
    Last Post: 08-18-2006, 10:33 AM
  5. When is the server being changed?
    By mithrandir in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 08-18-2001, 11:08 PM