A parameter is passed in an inefficient manner.
In C and C++, parameters are passed by value, that is, a call copies the contents of the actual parameter to the formal parameter as if by assignment. When a parameter is large, this copy is expensive. Usually it is better to pass arguments bigger than 16 bytes by reference, that is, to pass a pointer to the object rather than the object itself.
In C++, you can declare a REF parameter by replacing the formal parameter type T with T&. The call arguments and subroutine body are unaffected. In C, you can convert a by-value parameter to a by-reference parameter as follows. Declare the parameter as type T* rather than type T, replace all uses of the formal parameter "x" with "(*x)", and replace the corresponding argument V at each call by its address, &V.
Warning: Arguments passed by reference are modified if the called routine modifies the reference parameter. Before replacing a by-value parameter with a by-reference parameter, make sure there are no assignments to the formal parameter.
ID |
Observation |
Description |
---|---|---|
1 |
Definition |
This shows where the function was defined |
typedef struct { int x[10]; } T; int add_up(T p) { // Better is int add_up(T &p) int i, total = 0; for (i = 0; i < 10; i++) { total += p.x[i]; } return total; }
Copyright © 2010, Intel Corporation. All rights reserved.