1

In a C function, its locals's memory is allocated when the function is called, and deallocated when function is finished. What about for functions that return a value (e.g. int, string), when and where does the return address's memory is allocated and deallocated, and is it part of the call stack or the callee stack, or something else?

Consider the following example:

int* foo()
{
   int _myInt;
   return(&_myInt);
}

This example gets me completely confused as of how memory is allocated for the return address that return a pointer. Can someone please explain?

Same for C and Objective-C?

4
  • 2
    There isn't a simple correct answer. You can start with Calling convention and x86 calling conventions. Feb 11, 2012 at 6:22
  • Wait, are you asking about how parameters and results are passed or about ownership of memory returned by pointer? Feb 11, 2012 at 6:30
  • does return address pass by value or by reference?
    – KMC
    Feb 11, 2012 at 6:39
  • You code example returns a pointer by value, unfortunately the pointer contains the address of a function local object that no longer exists. Note that the pointer is probably returned in a register and not on the stack. Feb 11, 2012 at 6:58

4 Answers 4

4

I don't know about Objective-C but, with standard C, return values are typically stored in registers.

When the function returns a pointer type, it is up to the developer to ensure the memory pointed to remains valid after the call.

Consider the following function:

char* GetStr()
{
    char buff[50];
    strcpy(buff, "Hello, World!");
    return buff;
}

This function returns a pointer to local memory, which is deallocated when the function returns. Accessing that memory would constitute a bug with undefined behavior.

This also applies to your example, which is not valid. But since an int can fit in a register, you can simply do return _myInt; and return the value directly.

3
  • Which could be fixed by declaring the string buff as static char buff[50].
    – hotpaw2
    Feb 11, 2012 at 6:16
  • @hotpaw2: Or allocating the memory from the heap, or moving the declaration outside the function, or having the caller provider the buffer, etc. Feb 11, 2012 at 6:17
  • 1
    Yeah, of all the solutions, making the buffer static is probably the least preferable. It's out of the frying pan and into the fire. Feb 11, 2012 at 6:22
3

In Obj-C if you allocated an object via alloc call then the standard practice is to autorelease that object before returning and name the method is such a way that the caller knows that it is getting an autoreleased object. If the caller needs then it can retain that object again.

- (MyClass *)getMyAutoreleasedObject {
    MyClass *obj = [[MyClass alloc] init];
    return [obj autorelease];  // caller needs to retain if needed
}

And if you do not autorelease then you should name the method in such a way that the caller knows that it must release the returned object. This is a strictly followed convention in Obj-C.

In C, if you allocate memory in stack then that memory is not valid in caller.

int *func() {
    int a[100];
    return a;  // invalid in caller
}

Your example has the same problem. The returned memory is invalid in caller. In this case you can allocate the memory dynamically and the caller needs to free that when done.

int *func() {
    int *a = (int *) malloc (100 * sizeof(int));
    return a;  // caller must free(a)
}
3

Parameters are passed to a function, and return values are passed back to the caller. How that is done is totally up to the implementation. The language only requires that it works and has nothing to say about how to do it.

On systems with available registers and a hardware assisted stack, these are generally used for passing the values back and forth.

Different types might be treated differently, for example some CPUs have dedicated address registers that hold pointers, or separate floating point registers. In that case values of those types might be handled differently from integer or struct values.

The size of the values passed might also affect it, small value could fit in a register while larger values would not.

Note that a stack is not a requirement for an implementation, it just has to use some other method for organizing values and function calls.

0

In your example function, any use of the return value whatsoever will invoke undefined behavior, since the lifetime of the pointed-to object has ended. The only safe way to call this function is without using the return value so that it is immediately discarded.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.