473,805 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Writing Efficient Arguments

Pointer and reference arguments provide C++ programmers with the
ability to modify objects. What is more efficient, passing arguments
via pointer or reference? Avoid the stereotypical urge of debating
effective coding style. Instead, think of particular scenarios:
passing a 512MB object to function. Think behind the scenes, from the
stack and heap to the physical linking and build of the program.

Jul 9 '07 #1
35 1808
sp*******@gmail .com wrote:
Pointer and reference arguments provide C++ programmers with the
ability to modify objects. What is more efficient, passing arguments
via pointer or reference? Avoid the stereotypical urge of debating
effective coding style. Instead, think of particular scenarios:
passing a 512MB object to function. Think behind the scenes, from the
stack and heap to the physical linking and build of the program.

No way to tell. It's all platform-specific. If the implementation uses
pointers behind the scenes, there's likely little difference.

If it's important, profile. If not, then use the construct that most
clearly represents the intent of the program.

Brian
Jul 9 '07 #2
sp*******@gmail .com wrote:
Pointer and reference arguments provide C++ programmers with the
ability to modify objects. What is more efficient, passing arguments
via pointer or reference? Avoid the stereotypical urge of debating
effective coding style. Instead, think of particular scenarios:
passing a 512MB object to function. Think behind the scenes, from the
stack and heap to the physical linking and build of the program.
I propose that we ban the word "efficient" from newsgroup posts unless
the poster has benchmarked and shown that the micro-optimization he
wants actually may do something.
Jul 9 '07 #3
On Jul 9, 7:03 pm, spekyu...@gmail .com wrote:
Pointer and reference arguments provide C++ programmers with the
ability to modify objects. What is more efficient, passing arguments
via pointer or reference? Avoid the stereotypical urge of debating
effective coding style. Instead, think of particular scenarios:
passing a 512MB object to function. Think behind the scenes, from the
stack and heap to the physical linking and build of the program.
>From memory standpoint there is no difference. Assuming the address of
the reference and pointer are coming from the same location, I don't
believe there are additional instructions to set up the stack;
however, as soon as I say that, someone will cite some implementation
where it does.

The 'efficiency' is in the recipient. That is, it does not have to
check if a reference is null - since a reference can never be null.
Finally, I suspect if you declare a pointer parameter as 'void*', you
lose type-checking efficiency, or incur type-checking inefficiency
depending on your point-of-view.

Jul 10 '07 #4
On Jul 9, 7:22 pm, red floyd <no.s...@here.d udewrote:
spekyu...@gmail .com wrote:
Pointer and reference arguments provide C++ programmers with the
ability to modify objects. What is more efficient, passing arguments
via pointer or reference? Avoid the stereotypical urge of debating
effective coding style. Instead, think of particular scenarios:
passing a 512MB object to function. Think behind the scenes, from the
stack and heap to the physical linking and build of the program.

I propose that we ban the word "efficient" from newsgroup posts unless
the poster has benchmarked and shown that the micro-optimization he
wants actually may do something.
Alright, these methods perform the same exact tasks. In each case,
what will the cost in overhead be? Both implementations refer to an
object indirectly, which allows each method to make internal
modifications. Assume the object is referring to intelligence
photography or uncompressed video. Compare this with the an
inefficient implementation, what background details can you construct?

// SIZEOF(pObject) =4 bytes
// SIZEOF(pObject) =500 * 2^20 bytes
void function(myObje ct* pObject)
{
...
}

// SIZEOF(refObjec t) =500 * 2^20 bytes
void function(myObje ct& refObject)
{
...
}

// Inefficient Implementation
myObject function(myObje ct copy)
{
...
return copy;
}

Jul 10 '07 #5
On Jul 9, 7:11 pm, "Default User" <defaultuse...@ yahoo.comwrote:
spekyu...@gmail .com wrote:
Pointer and reference arguments provide C++ programmers with the
ability to modify objects. What is more efficient, passing arguments
via pointer or reference? Avoid the stereotypical urge of debating
effective coding style. Instead, think of particular scenarios:
passing a 512MB object to function. Think behind the scenes, from the
stack and heap to the physical linking and build of the program.

No way to tell. It's all platform-specific. If the implementation uses
pointers behind the scenes, there's likely little difference.

If it's important, profile. If not, then use the construct that most
clearly represents the intent of the program.

Brian
Are you suggesting this is compiler specific?

Jul 10 '07 #6
On Jul 9, 8:06 pm, gpuchtel <gpuch...@gmail .comwrote:
On Jul 9, 7:03 pm, spekyu...@gmail .com wrote:
Pointer and reference arguments provide C++ programmers with the
ability to modify objects. What is more efficient, passing arguments
via pointer or reference? Avoid the stereotypical urge of debating
effective coding style. Instead, think of particular scenarios:
passing a 512MB object to function. Think behind the scenes, from the
stack and heap to the physical linking and build of the program.
From memory standpoint there is no difference. Assuming the address of

the reference and pointer are coming from the same location, I don't
believe there are additional instructions to set up the stack;
however, as soon as I say that, someone will cite some implementation
where it does.

The 'efficiency' is in the recipient. That is, it does not have to
check if a reference is null - since a reference can never be null.
Finally, I suspect if you declare a pointer parameter as 'void*', you
lose type-checking efficiency, or incur type-checking inefficiency
depending on your point-of-view.
Alright, these methods perform the same exact tasks. In each case,
what will the cost in overhead be? Both implementations refer to an
object indirectly, which allows each method to make internal
modifications. Assume the object is referring to intelligence
photography or uncompressed video. Compare this with the an
inefficient implementation, what background details can you construct?

// SIZEOF(pObject) =4 bytes
// SIZEOF(pObject) =500 * 2^20 bytes
void function(myObje ct* pObject)
{
...
}

// SIZEOF(refObjec t) =500 * 2^20 bytes
void function(myObje ct& refObject)
{
...
}

// Inefficient Implementation
myObject function(myObje ct copy)
{
...
return copy;
}

Jul 10 '07 #7
sp*******@gmail .com wrote:
Pointer and reference arguments provide C++ programmers with the
ability to modify objects. What is more efficient, passing arguments
via pointer or reference? Avoid the stereotypical urge of debating
effective coding style. Instead, think of particular scenarios:
passing a 512MB object to function. Think behind the scenes, from the
stack and heap to the physical linking and build of the program.
Passing by reference automatically assumes only one object, whereas a
pointer may be to part of an array.
void someFnc1(MyObj &inp)
{
inp.doSomething ();
inp[1].doSomething(); // This is plain wrong
}

void someFnc2(MyObj *inp)
{
inp->doSomething( );
inp[1].doSomething(); // This is valid but maybe wrong and could
cause hard to debug problems
}

As for efficiency, I doubt with modern optimising compilers you would
find one more efficient than the other.

JB
Jul 10 '07 #8
On Jul 10, 1:22 am, red floyd <no.s...@here.d udewrote:
spekyu...@gmail .com wrote:
Pointer and reference arguments provide C++ programmers with the
ability to modify objects. What is more efficient, passing arguments
via pointer or reference? Avoid the stereotypical urge of debating
effective coding style. Instead, think of particular scenarios:
passing a 512MB object to function. Think behind the scenes, from the
stack and heap to the physical linking and build of the program.
I propose that we ban the word "efficient" from newsgroup posts unless
the poster has benchmarked and shown that the micro-optimization he
wants actually may do something.
I totally disagree. Programmer efficiency is very important,
and should only be compromized when absolutely essential. (Most
of the time, of course, we're really concerned with long term
programmer efficiency. We'll invest some additional effort up
front to make the inevitable maintenance easier.)

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 10 '07 #9
gpuchtel a écrit :
On Jul 9, 7:03 pm, spekyu...@gmail .com wrote:
>Pointer and reference arguments provide C++ programmers with the
ability to modify objects. What is more efficient, passing arguments
via pointer or reference? Avoid the stereotypical urge of debating
effective coding style. Instead, think of particular scenarios:
passing a 512MB object to function. Think behind the scenes, from the
stack and heap to the physical linking and build of the program.
[snip]
The 'efficiency' is in the recipient. That is, it does not have to
check if a reference is null - since a reference can never be null.
A reference can be NULL or invalid all the same. The difference is that
the code indicates a NULL wouldn't be expected (i.e. the code is self
explaining; whereas with pointer, you would have to read the doc/comments).

AFAIK the following code is valid (though UB):

void print(int& i)
{
cout<<&i<<endl;
}

int main()
{
print(*static_c ast<int*>(0));

return 0;
}

And prints '0'.

Michael
Jul 10 '07 #10

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

Similar topics

4
2088
by: Mark Stijnman | last post by:
A while ago I posted a question about how to get operator behave differently for reading and writing. I basically wanted to make a vector that can be queried about whether it is modified recently or not. My first idea, using the const and non-const versions of operator, was clearly not correct, as was pointed out. Julián Albo suggested I could use proxies to do that. I've done some googling for proxies (also in this group) and personally,...
4
1964
by: Peter Ammon | last post by:
Does anyone ever write the arguments to printf()-like functions under the corresponding format specifier? printf("My name is %s and I am %u years old\n", name, age); If the arguments are too long, you could put them on the next line printf("You got %u/%u (%.2f %%) right\n", numRight,
12
3772
by: Chris Springer | last post by:
I'd like to get some feedback on the issue of storing data out to disk and where to store it. I've never been in a production environment in programming so you'll have to bear with me... My question is about storing data in a database. Yes I understand that you can link to a database in your program and read and write to the database etc etc. Well, that's all find and dandy but what if the person you're writing the application for...
19
2544
by: Marco | last post by:
FYI: Guidelines for writing efficient C/C++ code http://www.embedded.com/showArticle.jhtml?articleID=184417272 any comments?
3
9243
by: patrickdepinguin | last post by:
Hi, I need to write large quantities of data to a file in C. The data comes from statistics that are continuously gathered from a simulator, and in order to not slow the whole thing down I would obviously want the writes to go as fast and efficient as possible. Since I/O operations are rather slow, I was thinking that using a large buffer would be better than writing each data point every time. Each data point calls my function, at which...
4
1583
by: Luna Moon | last post by:
seeking highly efficient caches scheme for demanding engineering computing? HI all, To same the time of costly function evaluation, I want to explore the possibility of caching. Suppose in millions of calls to the function evaluation, there are some computations, in which only a portion of the parameters change, others remain the same as the parameters that are used in some other function
8
4281
by: blaine | last post by:
Hey guys, For my Network Security class we are designing a project that will, among other things, implement a Diffie Hellman secret key exchange. The rest of the class is doing Java, while myself and a classmate are using Python (as proof of concept). I am having problems though with crunching huge numbers required for calculations. As an alternative I can use Java - but I'd rather have a pure python implementation. The problem is that...
2
2123
by: cj | last post by:
<System.Web.Services.WebService(Name:="CJ's Service", Description:="Testing 123", Namespace:="http://myco.com/cj/one")_ <System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _ <ToolboxItem(False)_ Public Class Service1 Inherits System.Web.Services.WebService <WebMethod()_ Public Function HelloWorld() As String Return "Hello World"
6
11733
by: L. Ximenes | last post by:
Greetings, I've recently been struggling with the idea of writing an XML document based on user-submitted content using javascript. Using various instances of document.write on a newly opened window I succeeded in the task of outputting a perfectly valid XML document, although I have been experiencing a few problems. If I look at the source code of the new window, what I have is a perfect XML document, such that if I copy the source...
0
9716
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
9596
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10360
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10366
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10105
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
9185
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
5542
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
4323
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
2
3845
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.