Thread: Pointers

  1. #1
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308

    Pointers

    Last night, I was writing a program to implement a Stack using Linked Lists which I learnt about recently at school but only had a vague idea about before that. Upon thinking (I might be overthinking this) more about my program, I feel that the compiler should complain "Out of Memory" or something of that sort. Can someone explain me why it isn't the case?

    Code:
    struct STUDENT
    {
           int ID;
           STUDENT* LINK;
    
    };
    
    int main()
    {
            STUDENT S;
    }
    Reason for why I feel this should give an error complaining out of memory is because "S" has a data member "ID" (an integer) and "LINK" which is a pointer of type "STUDENT". Now, "LINK" of "S" also has data members "ID" (an integer) and "LINK" which is a pointer of type "STUDENT" and this keeps going on and on.

    What I mean is:

    Code:
    S.ID=10;
    
    S.LINK->ID=20;
    
    S.LINK->LINK->ID=30;
    
    S.LINK->LINK->LINK->ID=40;
    
    //etc...
    
    //cout << S.ID <<" "<< S.LINK->ID and so on...
    
    /*This way there are basically infinitely many objects of type STUDENT and so as soon as we declare an object of type STUDENT and run the program, the compiler should report an error, shouldn't it?*/

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Maybe a good old analogy will help: suppose I give you a namecard containing my company's office's address. You put this namecard into your pocket. "That's impossible!" I exclaim. "How could you have put my company's office into your pocket?!"

    Sounds silly, but similiarly, you can put the address of an object into another object, and that doesn't mean that other object has the first object stored; it only stores an address.
    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

  3. #3
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308
    That makes sense but I still don't understand that if objects are allocated memory at run-time and each object stores the address of another object of the same type, isn't the existence of the other object supposed to be true for it to happen? And, so, if the other object does exist, you can say that this other object holds the address of yet another object and so the cycle continues on and on, effectively using up all of available heap memory.

    Let's say the total heap memory available is 26 MB and each company requires a space of 1 MB. You're from Company A and give me (I'm from company B) a card of your company effectively establishing some sort of a link between A and B. Now, I give a card of B (not because I want to but because I have to which is what, I guess, happens when we do what I mentioned in my first post) to someone from C because for B to exist, a C must exist. Similarly, for A to exist, a B must exist. And for C to exist, a D must exist and on and on and on until Z (where all 26 MB get used up) and there's no more memory left in heap resulting in an error when Z needs to create an object for itself to exist.

    I'm having a hard time getting my head wrapped around this, sorry! And also if my English is bad in the above description

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Zeus_
    Let's say the total heap memory available is 26 MB and each company requires a space of 1 MB. You're from Company A and give me (I'm from company B) a card of your company effectively establishing some sort of a link between A and B. Now, I give a card of B (not because I want to but because I have to which is what, I guess, happens when we do what I mentioned in my first post) to someone from C because for B to exist, a C must exist. Similarly, for A to exist, a B must exist. And for C to exist, a D must exist and on and on and on until Z (where all 26 MB get used up) and there's no more memory left in heap resulting in an error when Z needs to create an object for itself to exist.
    So, the pointer in Z can either be a null pointer (resulting in a typical singly linked list), or it could point back to A (resulting in a circular singly linked list).
    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

  5. #5
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308
    Well, in my program above, I'm not assigning the last objects data member pointer to NULL or neither am I linking it back to first object, so... I still feel there should be an error when there's none.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, so the last element has a pointer that just points to garbage, not to another element that points to another element etc. There's an end because at some point you have a pointer that is either a null pointer or points to an existing object, or is simply invalid.
    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

  7. #7
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308
    Makes sense. It also explains why I had two garbage values being shown when I first ran my linked list program and displayed the contents of each object. I later changed the code by adding a constructor that initially makes LINK point to NULL and it solved the issue.

    Code:
    struct STUDENT
    {
           int ID;
           STUDENT* LINK;
    
           STUDENT()
           {
                  LINK = NULL;
           }
    };
    Thanks @laserlight!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 08-29-2015, 01:15 PM
  2. Replies: 43
    Last Post: 05-23-2013, 03:01 PM
  3. size of struct with pointers and function pointers
    By sdsjohnny in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 05:19 AM
  4. Storing function pointers in generic pointers
    By Boxknife in forum C Programming
    Replies: 6
    Last Post: 08-01-2009, 01:33 PM
  5. weak pointers and use_count smart pointers
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 07-29-2006, 07:54 AM

Tags for this Thread