Skip to main content
gamedev.net
🔒 Locked

Function call with large structure.

Started by davidkosenina Jan 20, 2005 at 5:28 PM 3 replies 650+ views
Original Post
davidkosenina
davidkosenina
If i have very large structure and i pass it into a function as a parameter, does the compiler optimize this or does it pass all struct members on the stack? It is better to use pointers or references in terms of speed right? I was looking at some source code and i was wondering if it yust wasnt optimized or is this some "advanced stuff". Tnx.
C-Junkie
C-Junkie
It can't.

NOT passing it on the stack would mean the pass-by-value versus pass-by-reference semantics get messed up. That can't happen. (at least, I'm pretty sure C and C++ don't suddenly change how they work in the face of structures, unlike, say, java?)

Change the function to use a pointer or reference.
Anon Mike
Anon Mike
Typically in these cases you pass the struct by const reference (for C++).

foo(const BigStruct & stuff);

It looks the same at the call site (e.g. foo(stuff)) but doesn't have the copying.
-Mike
CGameProgrammer
CGameProgrammer
References are implicit pointers -- the generated output is identical to what it would be using pointers, but the C++ code is much cleaner. So use those. And you don't really need to make it a const as Anon Mike suggested... but you can.
~CGameProgrammer( ); Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.