C++ Programming - References - Discussion

Discussion :: References - General Questions (Q.No.1)


Which of the following statement is correct?

[A]. A reference is stored on heap.
[B]. A reference is stored on stack.
[C]. A reference is stored in a queue.
[D]. A reference is stored in a binary tree.

Answer: Option B


No answer description available for this question.

Amit said: (May 29, 2012)  
How can we say that, A reference is stored on stack. . ?
I think, Reference has no memory. It just an another name of variable.
Can anyone tell me. . ?

Saurabh said: (Jul 17, 2012)  
Actually cpp uses Reference model for user defined data types (like classes, enum etc) , so when a reference is created then actually no object is created in fact reference points to heap object and it is (reference) stored on stack.

Valsala Shukla said: (Sep 26, 2012)  
References is like pointers and pointers stores address so I think that's why references are stored on stack.

Praphulla said: (Feb 8, 2013)  
Reference is one type of variable stored on stack because of there is need to perform operation on them where object stored on heap.

Naresh said: (Mar 17, 2013)  
In c++ references are stored in stack surely.
When we talk about reference in java, it places Heap area.

Arpita Sahoo said: (Mar 28, 2013)  
What is constant?

Mahesh Hirpara said: (Apr 23, 2013)  
What say not possible reference stored on stack ? when it possible?

Muhammad Qamar Saleem said: (Jun 16, 2013)  
References store address of objects and only refer to objects. They are used only as a pointer.

Sunil Brahmajosyula said: (Sep 10, 2013)  
@valsala shukla.

Even pointers should be stored in queue?

Bindu said: (Sep 28, 2013)  
Reference type stored in heap not stack.

Chandan said: (Oct 21, 2013)  
A reference is stored on heap because dynamically allocation is stored in heap and reference create at run time. So yes its 100% right.

Stack is used to store local variable basically.

Sourabh K said: (Nov 26, 2013)  
Its true that reference is stored in stack.

Cause reference just point or refer to any local variable.

And any local variable has stack memory.

So Indirectly reference is stored in stack.

Ramesh said: (Dec 26, 2013)  
Reference is also a variable declared in a function. That should be stored in stack.

Velmurugan said: (Mar 13, 2014)  
Reference is a variable&data declaration part in your program. (i.e int a =6).

Wouter Van Ooijen said: (Sep 26, 2014)  
A reference is syntactic sugar for a pointer. It it can be stored either global, on the stack, or on the heap.

int a;
int &b{ a }; // global.

int &b{ a }; // on the stack.

struct ref{ int &b; ref( int &x ): b{ x }{} };
ref * p = new( a ); // p->b is on the heap.

Abc said: (Dec 26, 2014)  
Stack memory used by automatic variables, so this answer will true.

Abc said: (Dec 26, 2014)  
Stack memory used by automatic variables, the difference between reference variable.

Pointer is this Heap memory dynamically allocated at execution time.

Abc said: (Jan 13, 2015)  
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).

Rajendra said: (Jan 24, 2015)  
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?

Mohan said: (Aug 4, 2015)  
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.

Rajendra said: (May 2, 2017)  
ref in cpp is treated as constant pointer.

<type>* const <ptr>;

Divya said: (May 30, 2017)  
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.

Andrey said: (Oct 20, 2017)  
Reference like a restricted pointer.

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

#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;
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;

Vasanth Anderson said: (Aug 3, 2018)  
A reference variable is the "alias name" to the existing data type variable.

Harish said: (May 12, 2020)  
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.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.