简体   繁体   中英

function returning an instance of struct

struct Temp {};

struct Temp *newTemp() {
    struct Temp *temp = malloc(sizeof(struct Temp));
    return temp;
}


int main() {
    struct Temp *temp = newTemp(); // any problem?
    return 0;
}

Is there any problem with returning an instance of a struct from inside the function (factory method).

Will the pointer will be invalid. I was suggested that if you return a pointer it should point to something on the caller's stack, a global, or something on the heap

C structure must contain at least one member as per standard, but gcc allows it as an extension.

And what you did is legal - you can return pointer to dynamically allocated memory and use it in other function.

Memory allocated by malloc has storage duration (allocated storage duration) beyond the scope on which it is declared. You can use it without any problem.

It's expected that you would free the allocated memory and check the return value of malloc (saves you from dereferencing null in case of failure).

Standard N1570 C11 standard also mentions it under §7.22.3 memory management functions:

.... The lifetime of an allocated object extends from the allocation until the deallocation .

malloc allocates memory on heap. Objects allocated on heap has static storage duration. It is valid to return a pointer from a function to an object allocated by malloc .

Note that the empty struct you are using is not standard C. GCC provide this feature as an extension.

You have two ways to return a struct from a function:

  1. return a pointer to a dynamically allocated struct (your example code)

    The lifetime of a dynamically allocated object extends until it is de-allocated, so the caller will be able to use it. Simply you must not forget to free it when you no longer need it. Not doing so is called a memory leak. So a fixed version should be:

     struct Temp { int i; // a struct should not be empty... }; struct Temp *newTemp() { struct Temp *temp = malloc(sizeof(struct Temp)); return temp; } int main() { struct Temp *temp = newTemp(); // any problem? // use temp... free(temp); return 0; } 
  2. return a temporary object

    A struct is a first class object in C, so you can directly assign to it. You can then build a struct in a function and return it (beware: returning a pointer to it would actually return a dangling pointer because lifetime would end when function returns). So an alternate way is:

     struct Temp { int i; // a struct should not be empty... }; struct Temp newTemp() { struct Temp temp; // set values of temp members... return temp; } int main() { struct Temp temp = newTemp(); // no problem? // use temp... return 0; } 

    The good news here is that the struct is an automatic object in the caller, so it has not to (and shall not) be free-d. And decent compilers can even elide the copy by having the callee to directly use the caller automatic storage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM