C: Return value via stack/register question - c

I am new to C, and there is one thing I can not understand.
When function returns something that is not bigger than register -- my compiler puts it in EAX.
When I return big structure (not pointer but structure itself) -- it is returned via stack.
My question is: how compiler knows how to call function exported by another object?
There is a calling conventions (like stdcall) but it is about passing the arguments, not reading the returned value, right?
There should be some rule like "If return value declared to be larger than EAX, than take it from [bp-...]".
And one more: would it be right to say that objects I want to return, larger than register should be stored in heap and returned by pointer to prevent all than stack manipulations?
Thanks.

The way that the return value is passed to the caller is part of the function calling conventions as well. See here.
For example, regarding cdecl:
The cdecl calling convention is used
by many C systems for the x86
architecture. In cdecl, function
parameters are pushed on the stack in
a right-to-left order. Function return
values are returned in the EAX
register (except for floating point
values, which are returned in the x87
register ST0).
[...]
There are some variations in the
interpretation of cdecl, particularly
in how to return values. As a result,
x86 programs compiled for different
operating system platforms and/or by
different compilers can be
incompatible, even if they both use
the cdecl convention and do not call
out to the underlying environment.
Some compilers return simple data
structures with the length of 2
registers or less in EAX:EDX, and
larger structures and class objects
requiring special treatment by the
exception handler (e.g., a defined
constructor, destructor, or
assignment) are returned in memory. To
pass "in memory", the caller allocates
memory and passes a pointer to it as a
hidden first parameter; the callee
populates the memory and returns the
pointer, popping the hidden pointer
when returning.
The stack manipulations are going to be much much faster than the heap manipulations necessary if you allocate memory on the heap, so stack is always faster. The only reason (in C) you might want to return a pointer to something on the heap is because it won't fit on the stack.
Clarification:
In the last sentence above, "the only reason you might want..." should not be interpreted as "there is normally no reason to return a pointer". Rather, I mean "if you can do what you need without returning a pointer, the only reason to decide to use a pointer anyway is...".
Of course there are many valid reasons to return pointers from functions as Chris states in his own answer, but I 'm only talking about the cases where you don't need to do so.
In other words, return by value when you can; use pointers when you must.

And one more: would it be right to say that objects I want to return, larger than register should be stored in heap and returned by pointer to prevent all than stack manipulations?
Well, maybe. Honestly, the choice of "return by pointer" or "return by value" is one you should probably have better reasons to make than "I want the return to be faster." For example, it would be faster to return via pointer than via stack for large objects, but this isn't taking into account the greater amount of time it takes to allocate an object on the heap compared to the stack.
More importantly, return-by-pointer allows you to have opaque pointers, variably-sized objects and certain degrees of polymorphic behavior that are impossible in stack objects. If you want or need these kinds of behaviors, you should be using return-by-pointer anyway. If you don't, you can use return-by-value, or you can pass a pointer to an object allocated by the user (however they like) as a parameter and modify that parameter in your function (this is sometimes called an "out parameter" or something similar).
Choose the return method based on what you need and what your code does, not which you think is faster. If you find that you absolutely need the speed (after profiling and finding that the return is a bottleneck), then worry about this kind of micro-optimization.

Related

Do calling conventions prevent variable size return values?

As noted here, when returning variable-size data from a C function, you either:
Pass a pointer and max length. Return a flag indicating if max is reached.
Return a pointer to dynamically allocated data.
Return a pointer to global data.
Is there anything about calling conventions on typical hardware that prevent returning dynamic sized data via the call stack?
Some C compilers have VLAs (or alloca) so variable-size data on the call stack is possible. It would seem reasonably straightforward to implement a variable-size array return value by growing the caller's stack frame as if a VLA were declared instead of the function invocation. After returning, the callee would leave the element count, followed by the elements.
Before VLAs, we used the alloca standard library function, which does in effect return variable-size data. (It happens that the data is uninitialized but that's not essential to the implementation.) So evidently it is possible.
However, there is an important detail: alloca allocates storage each time it is called, and there is no freea. The only way to release the storage is to return from the function which calls alloca.
That's a plausible interface for local storage allocation, but not so great for a function which returns useful data. If you were to call functionWithVariableReturnSize() in a loop, you would end up with all the return values being saved in the stack frame until the frame is exited. That's probably not desirable, and it's probably not a good idea even if it matched some use case.
Variable-length arrays (which are an optional feature) are the only kind of dynamically-sized data structure C offers that has a source representation distinct from a pointer. C provides no mechanism even to express passing or returning arrays at all, however, because array-valued expressions decay to pointers in function argument lists and return statements. This is not a matter of calling convention, however, but rather deeply-rooted aspect of the language's design.
There are, of course, calling convention considerations that impact the implementation of function return values, but C implementations have had to work around the most significant of those already when it comes to functions returning structure types. If C supported returning arrays, then I don't think much new would be required on the calling convention front to support returning VLAs.

C: When to return by value or pass reference

Although the subject is discussed many times, I haven't found any satisfying answer so far. When to return data from a function by return or to pass a reference to change the data on address? The classic answer is to pass a variable as reference to a function when it becomes large (to avoid stack copying). This looks true for anything like a structure or array. However returning a pointer from a function is not uncommon. In fact some functions from the C library to the exact thing. For example:
char *strcat(char *dst, const char *src);
Always returns a pointer to destination even in case of an error. In this case we can just use the passed variable and leave the return for what it is (as most do).
When looking at structures I see the same thing happening. I often return pointers when functions only need to be used in variable initialization.
char *p = func(int i, const char *s);
Then there is the argument that stack coping variables is expensive, and so to use pointers instead. But as mentioned here some compilers are able to decide this themselves (assuming this goes for C as well). Is there a general rule, or at least some unwritten convention when to use one or the other? I value performance above design.
Start by deciding which approach makes the most sense at the logical level, irrespective of what you think the performance implications might be. If returning a struct by value most clearly conveys the intent of the code, then do that.
This isn't the 1980s anymore. Compilers have gotten a lot smarter since then and do a really good job of optimizing code, especially code that's written in a clear, straightforward manner. Similarly, parameter passing and value return conventions have become fairly sophisticated as well. The simplistic stack-based model doesn't really reflect the reality of modern hardware.
If the resulting application doesn't meet your performance criteria, then run it through a profiler to find the bottlenecks. If it turns out that returning that struct by value is causing a problem, then you can experiment with passing by reference to the function.
Unless you're working in a highly constrained, embedded environment, you really don't have to count every byte and CPU cycle. You don't want to be needlessly wasteful, but by that same token you don't want to obsess over how things work at the low level unless a) you have really strict performance requirements and b) you are intimately familiar with the details of your particular platform (meaning that you not only know your platform's function calling conventions inside and out, you know how your compiler uses those conventions as well). Otherwise, you're just guessing. Let the compiler do the hard work for you. That's what it's there for.
Rules of thumb:
If sizeof(return type) is bigger than sizeof(int), you should probably pass it by pointer to avoid the copy overhead. This is a performance issue. There's some penalty for dereferencing the pointer, so there are some exceptions to this rule.
If the return type is complex (containing pointer members), pass it by pointer. Copying the local return value to the stack will not copy dynamic memory, for example.
If you want the function to allocate the memory, it should return a pointer to the newly allocated memory. It's called the factory design pattern.
If you have more than one thing you want to return from a function - return one by value, and pass the rest by pointers.
If you have a complex/big data type which is both input and output, pass it by pointer.

How should I pass a struct in a function?

I am confused because I haven't written C in a while. In C++, we would pass them as references, in order not to copy the whole struct. Does this apply to C too? Should we pass them as pointers, even if we don't want to modify them, in order to avoid copying?
In other words, for a function that checks if two structs are equal, we better do
int equal(MyRecord* a, MyRecord* b);
and decrease a bit the readability (because of pointers)
or
int equal(MyRecord a, MyRecord b);
will have the same performance?
Often, passing pointers is faster - and you'll call equal(&r1, &r2) where r1 and r2 are local struct variables. You might declare the formals to be const pointers to a const structure (this could help the optimizing compiler to generate more efficient code). You might also use the restrict keyword (if you are sure you'll never call your equal with two identical pointers, e.g. equal(&r1,&r1), i.e. without pointer aliasing).
However, some particular ABIs and calling conventions may mandate particular processing for some few particular structures. For example, the x86-64 ABI for Linux (and Unix SVR4) says that a struct with two pointers or integral values will be returned thru two registers. This is usually faster than modifying a memory zone with its pointer in a register. YMMV.
So to know what is faster, you really should benchmark. However, passing a large-enough struct (e.g. with at least 4 integral or pointer fields) by value is almost always slower than passing a pointer to it.
BTW, what really matters on current desktop and laptop processors is the CPU cache. Keeping frequently used data inside L1 or L2 cache will increase performance. See also this.
What is faster massively depends on the size of the struct and it’s use inside the called function.
If your struct is not larger than a pointer, passing by value is the best choice (less or equal amount of data needs to be copied).
If your struct is larger than a pointer, it heavily depends on the kind of access taking place inside the called function (and appearantly also on ABI specifics). If many random accesses are made to the struct, it may be faster to pass by value, even though it’s larger than a pointer, because of the pointer indirection taking place inside the function.
All in all, you have to profile to figure out what’s faster, if your struct is larger than a pointer.
Passing pointers is faster, for the reasons you say yourself.
Actually, I find C more readable than C++ in this case: by passing a pointer in the call, you acknowledge that your paramters might get changed by the called function. With C++ references, you can't immediately say that by seeing only the call, you also have to check out the called function prototype to see if it uses references.

C programming: the cost of passing by value

I am learning C and get confused about something I read online.
At http://www.cs.bu.edu/teaching/c/stack/array/
I could read:
Let's look at the functions that determine emptiness and fullness.
Now, it's not necessary to pass a stack by reference to these
functions, since they do not change the stack. So, we could prototype
them as:
int StackIsEmpty(stackT stack);
int StackIsFull(stackT stack);
However, then some of the stack functions would take pointers (e.g.,
we need them for StackInit(), etc.) and some would not. It is more
consistent to just pass stacks by reference (with a pointer) all the
time
(I am not showing the code for what a stackT is, it is just a dynamic array)
From my (maybe limited) understanding, the disadvantage of passing by value is that the data is duplicated in the stack memory of the function. Since a stackT might be big, passing by value rather than pointer would be time consuming.
Do I get it right or am I still not clear with the basics ?
Correct, if you pass something "large" by value that item is copied onto the stack.
Passing a pointer to the data avoids the copy.
It is doubtful that the performance difference will be meaningful in most real-world applications, unless "large" is actually "huge" (which in turn may overflow the stack).
You are correct. Passing by value causes the program to copy, in the entirety, all of the data in that parameter. If it's only one or two ints, no problem, but copying multiple kilobytes is very expensive. Passing by reference only copies the pointer.
However, you must watch out for changing the data pointed at by the pointer and then expecting to return to it unchanged. C++ has passing by "const reference", which is like a guarantee that the data will not be changed, but C does not.

When to pass by value?

Dear all. I was wondering if there are examples of situations where you would purposefully pass an argument by value in C. Let me rephrase. When do you purposefully use C's pass-by-value for large objects? Or, when do you care that the object argument is fully copied in a local variable?
EDIT: Now that I think about it, if you can avoid pointers, then do. Nowadays, "deep" copying is possible for mostly everything in small apps, and shallow copying is more prone to pointer bugs. Maybe.
In C (sans const references), you pass by value for 3 reasons.
You don't want the source to be modified by the receiving function outside of its context. This is (was) the standard reason taught in school as it why to pass by value.
Passing by value is cheaper if the value fits within the architecture's register - or possibly registers if the compiler is very intelligent. Passing by value means no pointer creation and no dereference to get at the value being passed in. A small gain, but it does add up in certain circumstances.
Passing by value takes less typing. A weak reason to be sure, but there it is.
The const keyword negates most of reason 1, but reason 2 still has merit and is the main reason I pass by value.
Well, for one thing, if you want to change it.
Imagine the following contrived function:
int getNextCharAndCount (char *pCh);
Each time you call it, it returns the next most frequent character from a list by returning the count from the function and setting a character by way of the character pointer.
I'm having a hard time finding another use case which would require the pointer if you only ever wanted to use (but not change) the underlying character. That doesn't mean one doesn't exist of course :-)
In addition, I'm not sure what you're discussing is deep/shallow copy. That tends to apply to structures with pointers where a shallow copy just duplicates the top level while a deep copy makes copies of all levels.
What you're referring to is pass-by-value and pass-by-reference.
Passing by-reference is cheaper because you don't have to create a local copy of an object. If the function needs a local copy (for any purpose) - that could be a case.
I follow as a rule:
pass built-in types by value (int, char, double, float...)
pass classes and structs by (const) reference. There is no pointer handling involved whatsoever.
Never had any problems with this way of work.
If we're going to be pedantic about this, everyhing in C is pass-by-value. You may pass a pointer by value instead of passing the actual object by value, but it's still pass-by-value.
Anyway, why pass an entire object instead of a pointer to an object? Well, for one, your compiler may be able to optmize the call such that underneath the covers only an address is copied. Also/Alternatively, once you introduce pointers, your compiler may not be able to do as much optimization of your function because of aliasing. It's also less error prone to not have to remember to dereference. The caller can also be sure that what he passed in is not modified (const doesn't really guarantee this, it can be -dangerously- cast away)
I don't think your argument about chars holds water. Even though your char is conceptually 1 byte, each argument to a function call typically translates to a whole (word-sized) register and to the same amount of space on the stack for efficiency.
You can pass a whole struct on the stack as an argument if you really want to (and, I believe, return them as well). It's a way of avoiding both allocating memory and having to worry about pointer hygiene.
Depending on how the call stack is built the char and char* may take the same amount of space. It is generally better to have values aligned on word boundaries. The cost of accessing a 32 bit pointer on a word boundary may be significantly lower than accessing it on a non-word boundary.
Passing by value is safer if you don't want the value modified. Passing by reference can be dangerous. Consider passing by referennce
CONST int ONE = 1;
increment( *ONE );
print ONE;
Output is 2 if the constant was modified.

Resources