473,463 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

memory and passing pointers

Hi,

I'd like to ask if passing an object as an pointer into a function
evokes the copy constructor.

Ivan

Sep 29 '06 #1
8 2070
Ivan Liu wrote:
I'd like to ask if passing an object as an pointer into a function
evokes the copy constructor.
No. Copying pointers has no bearing on the object that they point to.
Sep 29 '06 #2
Thanks for the reply.

So the efficiency is as good as passing a reference? If so why pass
reference?
Ron Natalie wrote:
Ivan Liu wrote:
I'd like to ask if passing an object as an pointer into a function
evokes the copy constructor.

No. Copying pointers has no bearing on the object that they point to.
Sep 29 '06 #3
Ivan Liu wrote:
Thanks for the reply.

So the efficiency is as good as passing a reference? If so why pass
reference?

You are asking extremely vague questions. The main reasons references
exist are to accomodate overloading. If it were not for this C++
could have gotten by without them.

As for stylistically, it would depend on the situation. Nothing
is gained by switching from pointers to references blindly.
Sep 29 '06 #4
>>I'd like to ask if passing an object as an pointer into a function
>>evokes the copy constructor.
No. Copying pointers has no bearing on the object that they point
to.


So the efficiency is as good as passing a reference? If so why pass
reference?
Readability I'd say, although it is disputable. Some people prefer
passing pointers and some prefer passing reference. It's up to you to
chose your own style.

One place you really want to pass references instead of pointers is
overloaded operators.

Regards,
Ben
Sep 29 '06 #5
"Ivan Liu" <da*******@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
Ron Natalie wrote:
>Ivan Liu wrote:
I'd like to ask if passing an object as an pointer into a function
evokes the copy constructor.

No. Copying pointers has no bearing on the object that they point to.
Thanks for the reply.

So the efficiency is as good as passing a reference? If so why pass
reference?
Please don't top post in this newsgroup. Message rearranged.

The main reason I pass references into functions is so I can use . instead
of ->
Sep 29 '06 #6
Please don't top-post. Thank you. Rearranged.

Ivan Liu wrote:
Ron Natalie wrote:
Ivan Liu wrote:
I'd like to ask if passing an object as an pointer into a function
evokes the copy constructor.
No. Copying pointers has no bearing on the object that they point to.
Thanks for the reply.

So the efficiency is as good as passing a reference? If so why pass
reference?
There is a fundamental difference between passing references to objects
and passing pointers to objects. A reference must always refer to a
valid object. A pointer may refer to a valid object or may be null.
Therefore, if a function accepts a pointer, it must accept the
possibility of a null pointer and check the pointer value before using
it. Change the function to accept a reference and this extra
responsibility is removed. The function can safely assume that an
object is there.

The only time this is not an advantage is, of course, when, in your
design, the possibility of an object not existing makes sense. A
reference is no use here because it always referes to a valid object.
Pass a pointer and the function can query that pointer to see if it is
null or not.

A reference says: Here is the object for you to work with.
A pointer says: Here is an indicator that tells you whether the
optional object exists and, if so, where it is.

Summary: use references when you can and pointers when you must.

Gavin Deane

Sep 29 '06 #7
benben wrote:
>>I'd like to ask if passing an object as an pointer into a function
>>evokes the copy constructor.
>No. Copying pointers has no bearing on the object that they point
>to.
>

So the efficiency is as good as passing a reference? If so why pass
reference?

Readability I'd say, although it is disputable. Some people prefer
passing pointers and some prefer passing reference. It's up to you to
chose your own style.

One place you really want to pass references instead of pointers is
overloaded operators.

Two things about references that are not supported by pointers.

a) References can't be changed after being initialized
b) The lifetime of a reference can be transferred to objects.

(from an earlier post of mine)

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

}
Sep 29 '06 #8
Gavin Deane wrote:
A pointer may refer to a valid object or may be null.
Therefore, if a function accepts a pointer, it must accept the
possibility of a null pointer and check the pointer value before using
it.
That's certainly not true. The C++ standard library itself is
full of functions that assume (they are not required to check)
that the caller is not passing a NULL or otherwise invalid pointer.

>
Sep 29 '06 #9

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

Similar topics

32
by: John | last post by:
Hi all: When I run my code, I find that the memory that the code uses keeps increasing. I have a PC with 2G RAM running Debian linux. The code consumes 1.5G memory by the time it finishes...
5
by: Lionel | last post by:
Hi all, Just wondering exactly how memory is allocated using the new operator? More specifically I am interested in how the memory is calculated for globable variables? I recently stumbled into...
3
by: Pushker Pradhan | last post by:
I have a function which should allocate memory, initialize it to some values and return the address of the initialized memory to the calling function. void getWaveletCoeffs(float *ld, float *hd,...
13
by: hurry | last post by:
In order to avoid declaring static variables in a function I was asked to write a scratch memory. Reserve a block of memory outside the function and assigning pointers to the memory locations as...
94
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring...
2
by: Mike | last post by:
Hi, I am new to C and having problems with the following program. Basically I am trying to read some files, loading data structures into memory for latter searching. I am trying to use structres...
15
by: polas | last post by:
Hi everyone - I have a question. I am just playing around with C (I realise there are better ways to do what I want, but I would like to do it this way to increase my understanding of C) and would...
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
4
by: tech | last post by:
Hi, I need to pass a block of say 320 bytes memory between some classes which do various processing on it. The app needs to be quick so i can't keep copying. The simplest way is via pointer...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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
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
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
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.