Define a function returning a struct pointer

Please carry me, I'm from a different language and new to c and learn it from http://c.learncodethehardway.org/book/learn-c-the-hard-way.html

struct Person { char *name; int age; int height; int weight; }; struct Person *Person_create(char *name, int age, int height, int weight) { struct Person *who = malloc(sizeof(struct Person)); assert(who != NULL); who->name = strdup(name); who->age = age; who->height = height; who->weight = weight; return who; } 

I understand that the second Person_create function returns a pointer to a struct Person. I don't understand (maybe because I'm from another language, erlang, ruby), why does he define it as

 struct Person *Person_create(char *name, int age, int height, int weight) 

not

 struct Person Person_create(char *name, int age, int height, int weight) 

and is there another way to define a function to return a structure?

Sorry if this question is too simple.

+8
c
source share
5 answers

It is defined like this because it returns a pointer to a structure, not a structure. You assign the return value to the value of struct Person * , not struct Person .

You can return the full structure, for example:

 struct Person Person_create(char *name, int age, int height, int weight) { struct Person who; who.name = strdup(name); who.age = age; who.height = height; who.weight = weight; return who; } 

But it is not used very often.

+11
source share

The function returns who , which is a struct Person * - a pointer to a structure. The memory for storing the structure is allocated malloc() , and the function returns a pointer to this memory.

If a function is declared as a returned struct Person , and not a pointer, then who can also be declared as a structure. Upon return, the structure will be copied and returned to the caller. Note that a copy is less efficient than just returning a pointer to memory.

+3
source share

Structures are not default pointers (or references) in C / C ++, as they are, for example, in Java. The Struct Person () function should return the structure itself (by value, making a copy), and not a pointer.

You often don’t want to create copies of objects (shallow copies by default or copies created using copy constructors), as this can take a lot of time.

+2
source share

The Person_create function returns a pointer to a struct Person , so you need to define the return value as a pointer (by adding *). To understand the reason for returning a pointer to a structure, and not the structure itself, you need to understand how C processes memory.

When you call a function in C, you add an entry for it to the call stack . At the bottom of the call stack is the main function of the program in which you are working, at the top is the current function being executed. Entries in the stack contain information, such as parameter values ​​passed to functions, and all local function variables.

There is another type of memory that your program has access to: heap memory. Here you allocate space using malloc and it is not connected to the call stack.

When you return from a function, the call stack is called and all information associated with the function call is lost. If you want to return the structure, you have two options: copy the data inside the structure before it pops out of the call stack, or save the data in heap memory and return the pointer to it. Copying a data byte for a byte is more expensive than just returning a pointer, and therefore you would usually like to do this to save resources (both in memory and in the processor cycle). However, this does not happen without cost; when you store your data in heap memory, you should remember free , when you stop using it, otherwise your program will leak memory.

+2
source share

Copying the entire structure, not just the pointer, is less efficient, since the sizeof pointer is usually much smaller than the sizeof entire structure.

In addition, the structure may contain pointers to other data in memory and blind copying, which can be dangerous for dynamically distributed data (if the code processing one copy releases it, the other copy will remain with an invalid pointer).

So a small copy is almost always a bad idea if you are not sure that the original goes out of scope - and then why don’t you just return the pointer to the structure (the structure dynamically allocated on the heap, of course, so it won’t be destroyed, since objects allocated by the stack will be destroyed upon return from the function).

+1
source share

All Articles