C++ Programming - References - Discussion

Discussion Forum : References - General Questions (Q.No. 1)
1.
Which of the following statement is correct?
A reference is stored on heap.
A reference is stored on stack.
A reference is stored in a queue.
A reference is stored in a binary tree.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
25 comments Page 1 of 3.

Harish said:   4 years ago
The reference is stored in a stack while the object is allocated in the heap. Instances or references of a value type are stored in the stack.
(1)

Vasanth anderson said:   6 years ago
A reference variable is the "alias name" to the existing data type variable.
(1)

Andrey said:   7 years ago
Reference like a restricted pointer.

Reference can be stored on the stack or on the heap.
It is up to you in C++.

Example:
#include <stdlib.h>
#include <string>
#include <vector>

struct LinkedNode
{
LinkedNode(const std::string & val)
: value(val)
, next(*this)
{
}
LinkedNode(const std::string & val, LinkedNode & nextn)
: value(val)
, next(nextn)
{
}
bool islast() const
{
return (this == &next);
}
std::string value;
/**
* THIS REFERENCE CAN BE STORED ON THE HEAP OR ON THE STACK
**/
LinkedNode & next;
};

int main(int argc, char * argv[])
{
{
/***
* on stack
***/
LinkedNode n5("last node");
LinkedNode n4("node 4", n5);
LinkedNode n3("node 3", n4);
LinkedNode n2("node 2", n3);
LinkedNode n1("first", n2);
printf("%s\n", n5.islast() ? "true" : "false");
printf("%s\n", n1.islast() ? "true" : "false");
}
{
/***
* on heap
***/
LinkedNode * n5 = new LinkedNode("last node");
LinkedNode * n4 = new LinkedNode("node 4", *n5);
LinkedNode * n3 = new LinkedNode("node 4", *n4);
LinkedNode * n2 = new LinkedNode("node 4", *n3);
LinkedNode * n1 = new LinkedNode("first", *n2);
printf("%s\n", n5->islast() ? "true" : "false");
printf("%s\n", n1->islast() ? "true" : "false");
}
return 0;
}

Divya said:   7 years ago
If you see the memory management in c, local variables, function return addresses and formal arguments are stored in stack section.

So, reference is nothing but the address that's stored in stack section.

Rajendra said:   7 years ago
ref in cpp is treated as constant pointer.

<type>* const <ptr>;

Mohan said:   9 years ago
#include<iostream>
using namespace std;
class abc
{
int i = 10;
int &j = i;
int &g = i;
cout<<"size is"<<sizeof(class abc)<<endl;

};

Output will be 12. So it gets space in stack.
(1)

Rajendra said:   9 years ago
I read all above comments, I have a question regarding reference. Some people system it is stored on stack, some says it is stored on heap that means reference has physical existence in memory (stack/heap).

Then why many of the c++ books says that "it is always better to use reference instead of variable in function formal parameter list because it doesn't reserve memory". This statement means that reference doesn't have physical existence. How this contradiction get solved?

Abc said:   9 years ago
A pointer can be re-assigned any number of times while a reference can not be re-seated after binding.

Pointers can point nowhere (NULL) , whereas reference always refer to an object. You can't take the address of a reference like you can with pointers

There's no "Reference Arithmetic" (but you can take the address of an object pointed by a reference and do pointer arithmetic on it as in &obj + 5).

ABC said:   9 years ago
Stack memory used by automatic variables, the difference between reference variable.

Pointer is this Heap memory dynamically allocated at execution time.

ABC said:   9 years ago
Stack memory used by automatic variables, so this answer will true.


Post your comments here:

Your comments will be displayed after verification.