473,503 Members | 1,646 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Diff Reference b/w Pointer

HI all,
What are the major diff betweeb refernce and pointer in C++..

Cheers...

Jul 29 '05 #1
3 1509
Radde wrote:
What are the major diff betweeb refernce and pointer in C++..


What does your favourite C++ book say about them, and what don't
you understand there? Have you tried reading the FAQ on this
topic?
Jul 29 '05 #2
1.pointers can be null, references not
2. pointers can be dangling, references not.
3. pointers can be changed to point to other object, references not.
4. while passing parameters through pointers, copies of pointers are
made , 4 byte for each pointer on 32bit system, and while passing
parameters through refrences, compiler can optimize further and even
can save the 4 byte copy operation.
5. In previous case, while using pointer , you have to use & operator,
and using references, you save one key typing time.
6. When u will combine pointer and references, u can have a situtation
like this
int *p = new int;
int & q =*p;
......
int *r = &q;
delete r; ///somewhere in program
.........
q = 20; ///what is happening here?

Jul 29 '05 #3
upashu2 wrote:
1.pointers can be null, references not
2. pointers can be dangling, references not.
Doesn't point 6 show how references can be "dangling" ?

3. pointers can be changed to point to other object, references not.
4. while passing parameters through pointers, copies of pointers are
made , 4 byte for each pointer on 32bit system, and while passing
parameters through refrences, compiler can optimize further and even
can save the 4 byte copy operation.
5. In previous case, while using pointer , you have to use & operator,
and using references, you save one key typing time.
6. When u will combine pointer and references, u can have a situtation
like this
int *p = new int;
int & q =*p;
......
int *r = &q;
delete r; ///somewhere in program
.........
q = 20; ///what is happening here?


7. It is an error not to initialize a reference to a valid object.
8. References can increase the lifetime of a temporary to the lifetime
of the reference, pointers can not (see below).

#include <iostream>
#include <ostream>

struct A
{
A()
{
std::cout << "A default\n";
}

A( const A & )
{
std::cout << "A copy\n";
}

A & operator= ( const A & )
{
std::cout << "A operator =\n";
return * this;
}
~A()
{
std::cout << "A Destruct\n";
}
};
int main()
{
{
const A & a = A();

std::cout << "Temporary lives !\n";

A y( a );
}

std::cout << "Temporary is gone !\n";

{
const A * a = & A();

std::cout << "Temporary is gone already - a dangles!\n";
}
}
Jul 29 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
2510
by: bo | last post by:
And why and where one should use one vs. the other? Verbally, it seems like semantics to me--but obviously there is some actual difference that makes references different and or preferable over...
1
1488
by: r.devaraj | last post by:
Some typo and confusion in previous post (http://groups.google.com/group/comp.lang.c/browse_thread/thread/5e1f7437701250c4/c83fdc9a2d49e9ac?hl=en#c83fdc9a2d49e9ac) made it difficult for me to fetch...
18
3508
by: man | last post by:
can any one please tell me what is the diff between pointer and reference.....and which one is better to use ....and why???????
4
4933
by: Andreas Kasparek | last post by:
Hola! I'm preparing my master thesis about a XML Merge Tool implementation and was wondering if there is any open standard for XML diff regarding topics like: - is a diff result computed on...
6
1702
by: wenmang | last post by:
Here is the code which causes core dump when is built differently, 1 for regular dev built, the other for packaging build. class my_class{ public: my_class(){}; ~my_class(){}; private:...
17
1921
by: Rob Hoelz | last post by:
I've been compiling a list of the differences between C and C++, and I'd like to see how thorough I've been. Could any of you go over this list and see if I've missed anything? Here's the list:...
0
7198
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7072
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7319
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6979
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5570
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
373
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.