c++ - Pass a parameter by const reference to a non-void function : is it considered as a side effect? -


what cleanest way write function (not procedure) ?

does 2nd solution told have "side effect" ?

struct myarea {   int t[10][10]; // 100x100... }; 

solution 1 : pass value

 double mysum1(myarea a)  {    // compute , return sum of elements  } 

solution 2 : pass const reference

double mysum2(const myarea & a) {   // compute , return sum of elements } 

my prefered first 1 (clean function) although less effective. when there lot of data copy, can time-consuming.

thank feedback.

i have number of quibbles terminology:

  1. there's no such thing "procedure" in c or c++. @ best, there functions return no value: "void"

  2. your example has no "side effect".

  3. i'm not sure mean "clean function" ... hope don't mean "less source == cleaner code". nothing further truth :(

to answer original question:

  1. in example, double mysum1(myarea a) incurs space , cpu overhead of completely unnecessary copy. don't :)

  2. to mind, double mysum1(myarea & a) or double mysum1(myarea * a) equivalent. personally, i'd prefer double mysum1(myarea * a) ... c++ developers (rightly!) prefer double mysum1(myarea & a).

  3. double mysum1 (const myarea & a) best of all: has runtime efficiency of 2), , signals intent won't modify array.

ps: generated assembly output following test:

struct myarea {   int t[10][10]; };  double mysum1(myarea a) {   double sum = 0.0;   (int i=0; < 10; i++)     (int j=0; j<10; j++)       sum += a.t[i][j];   return sum; }  double mysum2(myarea & a) {   double sum = 0.0;   (int i=0; < 10; i++)     (int j=0; j<10; j++)       sum += a.t[i][j];   return sum; }  double mysum3(myarea * a) {   double sum = 0.0;   (int i=0; < 10; i++)     (int j=0; j<10; j++)       sum += a->t[i][j];   return sum; }  double mysum4(const myarea & a) {   double sum = 0.0;   (int i=0; < 10; i++)     (int j=0; j<10; j++)       sum += a.t[i][j];   return sum; } 

mysum1, you'd expect, had code copy.

the output mysum2, mysum3 , mysun4, however, identical:

_z6mysum2r6myarea: .lfb1:     .cfi_startproc     .cfi_personality 0x3,__gxx_personality_v0     pushq   %rbp     .cfi_def_cfa_offset 16     .cfi_offset 6, -16     movq    %rsp, %rbp     .cfi_def_cfa_register 6     pushq   %rbx     movq    %rdi, -32(%rbp)     movl    $0, %eax     movq    %rax, -24(%rbp)     movl    $0, -16(%rbp)     jmp .l8     .cfi_offset 3, -24 .l11:     movl    $0, -12(%rbp)     jmp .l9 .l10:     movl    -16(%rbp), %eax     movl    -12(%rbp), %edx     movq    -32(%rbp), %rcx     movslq  %edx, %rbx     movslq  %eax, %rdx     movq    %rdx, %rax     salq    $2, %rax     addq    %rdx, %rax     addq    %rax, %rax     addq    %rbx, %rax     movl    (%rcx,%rax,4), %eax     cvtsi2sd    %eax, %xmm0     movsd   -24(%rbp), %xmm1     addsd   %xmm1, %xmm0     movsd   %xmm0, -24(%rbp)     addl    $1, -12(%rbp) .l9:     cmpl    $9, -12(%rbp)     setle   %al     testb   %al, %al     jne .l10     addl    $1, -16(%rbp) .l8:     cmpl    $9, -16(%rbp)     setle   %al     testb   %al, %al     jne .l11     movq    -24(%rbp), %rax     movq    %rax, -40(%rbp)     movsd   -40(%rbp), %xmm0     popq    %rbx     leave     .cfi_def_cfa 7, 8     ret     .cfi_endproc   <= mysum3 , mysum4 had different labels ... identical instructions! 

it's worth noting 1 of benefits of "const" can compiler perform several different kinds of optimizations, whenever possible. example:


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -