C++ Programming - References - Discussion
Discussion Forum : References - General Questions (Q.No. 1)
1.
Which of the following statement is correct?
Discussion:
25 comments Page 2 of 3.
Rajendra said:
8 years ago
ref in cpp is treated as constant pointer.
<type>* const <ptr>;
<type>* const <ptr>;
Rajendra said:
1 decade 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?
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:
1 decade 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).
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:
1 decade ago
Stack memory used by automatic variables, the difference between reference variable.
Pointer is this Heap memory dynamically allocated at execution time.
Pointer is this Heap memory dynamically allocated at execution time.
ABC said:
1 decade ago
Stack memory used by automatic variables, so this answer will true.
Wouter van ooijen said:
1 decade ago
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.
f(){
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.
int a;
int &b{ a }; // global.
f(){
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.
Velmurugan said:
1 decade ago
Reference is a variable&data declaration part in your program. (i.e int a =6).
Sunil brahmajosyula said:
1 decade ago
@valsala shukla.
Even pointers should be stored in queue?
Even pointers should be stored in queue?
Muhammad Qamar Saleem said:
1 decade ago
References store address of objects and only refer to objects. They are used only as a pointer.
Mahesh Hirpara said:
1 decade ago
What say not possible reference stored on stack ? when it possible?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers