Thread: Newbie Pointer question

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    7

    Newbie Pointer question

    Hi-
    What's the diffreence between doing this:
    Code:
    Node *temp;
    temp = new Node;
    temp->data=x;
    and this:
    Code:
    Node *temp;
    Node n;
    n.data = x;
    temp = &n;
    When I use the first one, I get the expected results for temp, so I guess that's correct, but I don't understand why the second one wouldn't also work.

  2. #2
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    How wouldn't the second one work? Assuming you're doing everythng else right, the code you've posted is sound. Most likely you're trying to use local variables to build something like a linked list, and that usually ends up being an aliasing problem because all of the nodes point to the same memory.

    Cheers!

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The second snippet works, standing alone. But n will disappear once you leave the function you're in (presumably LinkedList::addNode or something similar), at which point temp will point to invalid memory. Allocating the node on the heap (with new) will prevent that.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    7
    yep, that was it - once the variable n gets descoped, I get all kinds of weird results. makes sense

    Thanks!

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You have no idea how much an advantage you have over most newbies for understanding that.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    7
    Quote Originally Posted by CornedBee
    You have no idea how much an advantage you have over most newbies for understanding that.
    Thanks.

    Actually, I have 5 years programming experience in the java/web world. I'm just trying to get into desktop programming. Have to think back to my cs undergrad courses. It's hard getting back into the lower-level stuff and the syntax of C++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning pointer from function question
    By Rune Hunter in forum C++ Programming
    Replies: 12
    Last Post: 07-12-2007, 09:45 AM
  2. Pointer question
    By rakan in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2006, 02:23 AM
  3. memory question (pointer related)
    By cjschw in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2004, 01:09 PM
  4. general pointer & malloc question
    By jstn in forum C Programming
    Replies: 2
    Last Post: 05-14-2002, 09:51 AM
  5. Newbie Q - Pointer vs None pointer references.
    By Guardian in forum Windows Programming
    Replies: 6
    Last Post: 05-02-2002, 04:52 PM