473,625 Members | 2,770 Online
Bytes | Software Development & Data Engineering Community
+ 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 2090
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*******@gmai l.comwrote in message
news:11******** **************@ b28g2000cwb.goo glegroups.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
3833
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 execution. But I do not think it needs so much memory. About 500M memory should be enough. I have following questions about memory leak. (1).If in my code I only define constructor for my class, and do not define destructor, will it cause memory leak?
5
1688
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 a problem which corrected my way of thinking. Now, people probably won't be able to answer much to that question, so I will give an example. This is example uses the qt library qstring.h, but I assume this would be similar to the C++ string.h...
3
2400
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, int *filterLen) { ld = (float*)malloc(2*SZFLOAT); hd = (float*)malloc(2*SZFLOAT); *ld++ = 0.7071; *ld-- = 0.7071;
13
6141
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 per convenience and access them. I was told this would save some memory. I dont understand the logic behind this, as i`ve declared variables as global (assuming i`ve declared the block in main() ) this would always b a residual data for access at...
94
4699
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 that I found inside of a string. Any ideas?
2
3269
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 and arrays of pointers to them. I have gotten the program to compile with gcc on WinXP. If the file i read doesnt have alot of records, it runs thru. But once i add more, it dies. In this program i have 4 files setup to read. The
15
2198
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 like to read an executable file in to a portion of memory and then pass execution to this and execute the file. However, I can not get it working and my efforts have resulted in a Seg Fault. Below is the code I have got #include "stdio.h"
11
3342
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 ways of passing an array. In the following code, fun1(int a1) - same as fun1(int* a1) - where both are of the type passed by reference. Inside this function, another pointer a1 is created whose address &a1 is different from that of the passed...
4
1798
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 say: class A {
0
8253
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8692
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8497
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7182
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5570
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4089
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2621
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1499
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.