Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code> Node *cursor; cursor = new Node; cursor = begin; </code></pre> <p>Understand exactly what you are doing with these 3 lines here - there might be more going on than you actually realise. </p> <ul> <li><p>On the first line, you declare a new variable called <em>cursor</em> - this variable is a name for a pointer object which has a type of <strong>Node*</strong>. (in plain english, Node* means "pointer-to <em>Node</em>").<br> The type of data stored in a pointer object is simply numeric integral data which happens to represent an address in memory. a pointer object is really no different to any other kind of integer object.<br> (Note the difference between "variable" and "object" - an object is something stored in memory, such as a number/a character/a memory address, and a variable is a <strong><em>name</em></strong> for an object)</p></li> <li><p>On the second line, two things happen - firstly you allocate memory for a <strong>Node</strong> object and construct a new <strong>Node</strong> object in that allocated memory - this memory does not have a variable name, it's just a free-store object (That's the short version of how <em>new Node</em> works), Then you use the = operator (assignment operator) to store the address value (a number) of the newly allocated <strong>Node</strong> object in your <em>cursor</em> variable.<br> After storing the address value of the new Node object, the only way to access the newly allocated Node object is through the cursor variable name.</p></li> <li><p>On the third line, you immediately overwrite the value stored by your cursor variable; instead of storing the address data for the new Node, it stores the address of another object (The address of that other object copied from another pointer variable called <em>begin</em>). This has the immediate effect of causing the newly allocated <strong>Node</strong> to be "Leaked".</p></li> </ul> <p>The point which I am trying to put across (Which I believe you might not realise) is that there are potentially four distinct objects (i.e. "items in memory") to consider in those three lines of code. Two of them are pointer objects (integers) called <em>cursor</em> and <em>begin</em>, the other two are free-store objects (Nodes) which have no name, but are accessible using the address values stored by your pointer objects. </p> <p>There are a bunch of links which you might find worth reading here:</p> <ul> <li><p><a href="http://www.eternallyconfuzzled.com/tuts/languages/jsw_tut_pointers.aspx" rel="nofollow">http://www.eternallyconfuzzled.com/tuts/languages/jsw_tut_pointers.aspx</a></p></li> <li><p><a href="http://www.augustcouncil.com/~tgibson/tutorial/ptr.html" rel="nofollow">http://www.augustcouncil.com/~tgibson/tutorial/ptr.html</a></p></li> <li><p><a href="http://www.daweidesigns.com/cgi-bin/pointers.php" rel="nofollow">http://www.daweidesigns.com/cgi-bin/pointers.php</a></p></li> <li><p><a href="http://www.c-faq.com/ptrs/index.html" rel="nofollow">http://www.c-faq.com/ptrs/index.html</a></p></li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload