Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
577 views
in Technique[技术] by (71.8m points)

performance - c++ passing arguments by reference and pointer

in c++

class bar
{
    int i;
    char b;
    float d;
};

void foo ( bar arg );
void foo ( bar &arg );
void foo ( bar *arg );

this is a sample class/struct and functions
i have some Qs

  • what's the difference between 1st and 2nd way of passing the argument in 'asm', size, speed ?
  • how the arguments are passed to the functions foo in each case ( in case of pointer i know the pointer is pushed on the stack )
  • when passing arguments, in terms of efficiency at ( speed, size, preferability ) which is better ?
  • what's the intel 'asm' syntax that corresponds each of the ways of passing arguments ?

i know what most say about "it doesn't matter on modern compilers and CPUs" but what if we're talking about Old CPUs or compilers?

thanks in advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The pointer and the reference methods should be quite comparable (both in speed, memory usage and generated code).

Passing a class directly forces the compiler to duplicate memory and put a copy of the bar object on the stack. What's worse, in C++ there are all sort of nasty bits (the default copy constructor and whatnot) associated with this.

In C I always use (possibly const) pointers. In C++ you should likely use references.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...