if i write this code:
Node* head;
I understand head is a variable type of Node and it stores a variable’s address type of Node.
Is that true?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
First lets get some definitions straight.
What this does is allocate space on the stack for a pointer. Note that only the space for the pointer is allocated, not space for the data. Now
Nodeis the type. That tells the compiler what data is at that location. It also tells the compiler how to interact with the data.This allocates space for the pointer on the stack. It then allocates space in the heap and returns the address (the index) to be stored in the space on the stack. Remember that data on the stack disappears after the function returns, so if you don’t call
deleteto get rid of the data in the heap or pass the reference on, when the function returns you have no way to access the data or delete it; this is called a memory leak.This declaration, however, allocates space for all of the data for the Node object on the stack. The main difference between this and the allocation to the heap (one example above) is that this variable is LOCAL. I.e. it will disappear after the current function returns.