References vs. Pointers
-
I have three questions? ONE: It is said "using references is safer than using pointer." Why? TWO: If I want node objects in heap, how can I assign the pointers from heap memory? example code) Node& Nodes[100]; ? = new Node(); THREE: Is using pointers faster than using references? Thanks!
Yonggoo
N
-
I have three questions? ONE: It is said "using references is safer than using pointer." Why? TWO: If I want node objects in heap, how can I assign the pointers from heap memory? example code) Node& Nodes[100]; ? = new Node(); THREE: Is using pointers faster than using references? Thanks!
Yonggoo
N
Yonggoo wrote:
using references is safer than using pointer
Because a pointer can be NULL. A reference means the object has to exist (of course, that object may not be fully initialized, but it does mean you don't have to worry about a NULL pointer.) I don't understand what you are doing with two.
Node* pNode = new Node;
Don't know what the references line has to do with anything.Yonggoo wrote:
Is using pointers faster than using references?
With an optimizing compiler the performance is generally the same since internally references are pointers.
Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke
-
Yonggoo wrote:
using references is safer than using pointer
Because a pointer can be NULL. A reference means the object has to exist (of course, that object may not be fully initialized, but it does mean you don't have to worry about a NULL pointer.) I don't understand what you are doing with two.
Node* pNode = new Node;
Don't know what the references line has to do with anything.Yonggoo wrote:
Is using pointers faster than using references?
With an optimizing compiler the performance is generally the same since internally references are pointers.
Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke
-
Is this code OK! Node* Nodes[101]; refNodes& getNodes() { for(int i=1;i<=100; ++i) { Nodes[i] = new Node(); } return Nodes; } Thanks!
Yonggoo
Yes, but you've failed to initialize
Nodes[0]
. The preferred style is:for (int i = 0; i < 101; ++i)
...
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
Is this code OK! Node* Nodes[101]; refNodes& getNodes() { for(int i=1;i<=100; ++i) { Nodes[i] = new Node(); } return Nodes; } Thanks!
Yonggoo
No, since you can't convert Node*[101] to Node&. You could return Nodes[101] as a Node**. I'm not sure how you would return it as a reference or why you would bother.
Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke
-
I have three questions? ONE: It is said "using references is safer than using pointer." Why? TWO: If I want node objects in heap, how can I assign the pointers from heap memory? example code) Node& Nodes[100]; ? = new Node(); THREE: Is using pointers faster than using references? Thanks!
Yonggoo
N
Yonggoo wrote:
Node& Nodes[100]; ? = new Node();
I don't think you can have an array of references
Yonggoo wrote:
Node* Nodes[101]; refNodes& getNodes() { for(int i=1;i<=100; ++i) { Nodes[i] = new Node(); } return Nodes; }
return Nodes; is returning a Node**. Is that what a refNodes& is? Mark
"Do you know what it's like to fall in the mud and get kicked... in the head... with an iron boot? Of course you don't, no one does. It never happens. It's a dumb question... skip it."
-
Yonggoo wrote:
using references is safer than using pointer
Because a pointer can be NULL. A reference means the object has to exist (of course, that object may not be fully initialized, but it does mean you don't have to worry about a NULL pointer.) I don't understand what you are doing with two.
Node* pNode = new Node;
Don't know what the references line has to do with anything.Yonggoo wrote:
Is using pointers faster than using references?
With an optimizing compiler the performance is generally the same since internally references are pointers.
Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke
Joe Woodbury wrote:
Because a pointer can be NULL. A reference means the object has to exist (of course, that object may not be fully initialized, but it does mean you don't have to worry about a NULL pointer.)
That is a red herring...
struct A {
int a;
};void func1(A& r)
{
printf("r.a = %d\n", r.a);
}void func2(A* p)
{
func(*p);
}
...
A* p = 0;
func2(p);Guess where it'll blow up on you in virtually all C++ compilers? References in C++ are not safer than pointers. It's just an illusion...
-
Joe Woodbury wrote:
Because a pointer can be NULL. A reference means the object has to exist (of course, that object may not be fully initialized, but it does mean you don't have to worry about a NULL pointer.)
That is a red herring...
struct A {
int a;
};void func1(A& r)
{
printf("r.a = %d\n", r.a);
}void func2(A* p)
{
func(*p);
}
...
A* p = 0;
func2(p);Guess where it'll blow up on you in virtually all C++ compilers? References in C++ are not safer than pointers. It's just an illusion...
Joergen Sigvardsson wrote:
void func2(A* p) { func(*p); }
The only reason it blows up is because you are dereferencing a NULL pointer, not because references are not safe. And one wonders why so many products have stupid warning labels; to prevent them from being used in ways they were not intended to be used.
You may be right
I may be crazy
-- Billy Joel --Within you lies the power for good, use it!!!
-
Joergen Sigvardsson wrote:
void func2(A* p) { func(*p); }
The only reason it blows up is because you are dereferencing a NULL pointer, not because references are not safe. And one wonders why so many products have stupid warning labels; to prevent them from being used in ways they were not intended to be used.
You may be right
I may be crazy
-- Billy Joel --Within you lies the power for good, use it!!!
PJ Arends wrote:
The only reason it blows up is because you are dereferencing a NULL pointer, not because references are not safe.
My point is that you can't trust a reference to be always valid, as many people suggest.
-- Fun for the whole family - except grandma and grandpa