C++ Programming - References - Discussion
Discussion Forum : References - General Questions (Q.No. 1)
1.
Which of the following statement is correct?
Discussion:
25 comments Page 3 of 3.
Rajendra said:
8 years ago
ref in cpp is treated as constant pointer.
<type>* const <ptr>;
<type>* const <ptr>;
Divya said:
8 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.
So, reference is nothing but the address that's stored in stack section.
Andrey said:
8 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;
}
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;
}
Vasanth anderson said:
7 years ago
A reference variable is the "alias name" to the existing data type variable.
(1)
Harish said:
5 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)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers