c++ - Dyamic memory & Linked Lists -


okay suppose have linked list implementation in c++. furthermore suppose node class , list class , looks this:

#include <iostream>  using namespace std;  class list;  class node { private:     node(char, node*);     char data;     node* next;     friend class list;     friend ostream& operator<<(ostream&, const list&); };  class list { public:     list(int = 0);     list(const list&);     ~list();     bool gotobeginning();     bool gotoend();     bool gotonext();     bool gotoprior();     bool insert(char);     bool remove(char&);     bool empty() const;     bool full() const;     bool clear();     list& operator=(const list&);     friend ostream& operator<<(ostream&, const list&);  private:     node* head;     node* cursor; };  #endif 

suppose list empty. furthermore, suppose inserted new element, 'z', list means list has 1 node currently.

lets take @ insert function:

bool list::insert(char item) {     if(empty())     {         node* newnode = new node(item, null);         head = cursor = newnode;         return true;     }     else     {         // dont need show part because above code suffice     } } 

okay know variable newnode of type pointer node gets assigned memory location containing node has been stored on heap.

now remember created new node , letts suppose item passed insert function 'z'

     memory allocation stack             heap     |               |     |               |    \|/             \|/   512               902            memory address | -----|   |--->|-----|-----| |  902 |   |    |  'z'| null|                     |------|---|    |-----|-----| newnode        (no name/ newnode)   variable name 

(no name): because memory allocated on heap can not directly accessed other pointer.

the problem i'm having this. newnode pointer created on stack , assiged memory address 512 shown above? or pointer never allocated on stack (so remove 512 above) , pointing memory (902) creted on heap?

the reason asking because second line of code inside of if statement inside of insert function assigns newnode both head , cursor

thats whats confusing me. head , cursor contain address 512 or 902?

also if continue , write code inside of else statement inside of insert function this:

bool list::insert(char item) {     if(empty())     {         node* newnode = new node(item, null);         head = cursor = newnode;         return true;     }     else     {         node* newnode = new node(item, null);         cursor = cursor->next = newnode; // line doing         return true;     }      return false; } 

so how cursor->next value of new node , cursor value of new node.

and yes above function works fine , got on project of code right concepts mentioned troubling me

the pointer returned new needs stored somewhere, , it's stored in stack-variable newnode. value of newnode, contents, address returned new.

when assign pointer pointer, it's value of pointer, contents, address pointer points to, copied.

so when e.g.

head = newnode; 

then head same value newnode, , point memory allocated new expression.


lets take small example: have 2 pointers:

int* a; int* b; 

you allocate memory , assign a:

a = new int; 

now memory looks this:

 +---+     +------------------+ | | --> | allocated memory | +---+     +------------------+ 

then assign a b:

b = a; 

then memory looks this:

 +---+ | | -. +---+  |    +------------------+         >-> | allocated memory | +---+  |    +------------------+ | b | -' +---+ 

exactly variables a , b stored doesn't matter, neither matter pointing.


Comments