Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning a struct pointer

Tags:

c

pointers

struct

Suppose I have the following struct and function returning a pointer:

typedef struct {   int num;   void *nums;   int size; } Mystruct;  Mystruct *mystruct(int num, int size) {     //Is the following correct? Is there a more efficient way?    Mystruct mystruct;    mystruct.num = num;    mystruct.size = size;    mystruct.nums = malloc(num*sizeof(size));    Mystruct *my;    *my = mystruct;    return my; } 

I want to define any Mystruct pointer using the above function. Should I declare a Mystruct variable, define the properties of Mystruct, assign a pointer to it, and return the pointer or define the properties of a mystruct property through a pointer immediately?

like image 614
idealistikz Avatar asked Apr 24 '10 02:04

idealistikz


People also ask

Can you return a struct pointer in C?

Use Standard Notation to Return struct From Function Now, functions in C can return the struct similar to the built-in data types. In the following example code, we implemented a clearMyStruct function that takes a pointer to the MyStruct object and returns the same object by value.

Should I return a struct or a pointer?

There are two ways of "returning a structure." You can return a copy of the data, or you can return a reference (pointer) to it. It's generally preferred to return (and pass around in general) a pointer, for a couple of reasons. First, copying a structure takes a lot more CPU time than copying a pointer.

Can you return pointers?

We can pass pointers to the function as well as return pointer from a function. But it is not recommended to return the address of a local variable outside the function as it goes out of scope after function returns.

Can you return a pointer in C++?

Return Pointer from Functions in C++ Second point to remember is that, it is not good idea to return the address of a local variable to outside of the function, so you would have to define the local variable as static variable.


2 Answers

Should I declare a Mystruct variable, define the properties of Mystruct, assign a pointer to it, and return the pointer

Definitely not, because the variable defined in the function (in "auto" storage class) will disappear as the function exits, and you'll return a dangling pointer.

You could accept a pointer to a Mystruct (caller's responsibility to allocate that) and fill it in; or, you can use malloc to create a new one (caller's responsibility to free it when it's done). The second option at least lets you keep the function signature you seem to be keen on:

Mystruct *mystruct(int num, int size) {    Mystruct *p = malloc(sizeof(MyStruct));    ....    return p; } 

but it's often an inferior one -- since the caller has to have responsibilities anyway, may as well go with the first option and potentially gain performance (if the caller can use an auto-class instance because it knows the scope of use is bounded).

like image 80
Alex Martelli Avatar answered Sep 19 '22 15:09

Alex Martelli


You can't use the variable because it will be deallocated when the function exits. For example:

Mystruct *mystruct(int num, int size) {    MyStruct x;    x.num = 1;    ...    return &x; } 

Will give a segmentation fault or access violation because the memory for x is deallocated as soon as you exit. So you have to allocate memory for the struct (and be sure to free it up later) or declare a global that will stay around for ever. Example for the latter...

Mystruct *mystruct(int num, int size) {    MyStruct *x;    x = (MyStruct*)malloc( sizeof( MyStruct ) );    x->num = 1;    ...    return x; } 
like image 44
staticman Avatar answered Sep 19 '22 15:09

staticman