Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

The drawbacks of passing arguments by const

Status
Not open for further replies.

Kounista

Junior Member level 2
Joined
Jul 15, 2009
Messages
22
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Tunisia
Activity points
1,500
Hi everyone,


I have a general question about code optimization for all the software experts here: I know that passing arguments by const instead of value is more efficient and allows us to avoid allocating a temporary local variable of the argument type. But is this always true? Or are there some cases when calling functions with constant arguments should be avoided? If so, is passing by pointer the most efficient way?


Thanks in advance and I hope my question is clear!
 

I think this will depend somewhat on the compiler you use, but I don't know of any reasons calling functions with consts should be avoided. In general, passing by pointer is more efficient than passing variables. The best way to know is compile different code and check the disassembly. A pointer might in some cases be less efficient, depending on the type being passed, e.g. if the pointer is 4 bytes and you are only passing one byte, it might be better to just pass the variable. Again, I would check what the compiler does in each case.
 

For small amount of data, I presume there are no issues concerning to the increment on access time employing one or other option due this would represent an insignificant delay if compared to overall program, but for bulk transfer would require either perform a simulation or disassemble the C code into Assembly to check which instructions were used to do that, and result could depend on uC core ( RISC, CISC ).
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top