6

I've got a question to C structs and datatypes. I have a struct called test:

struct test
{
    char* c;
    char* c2;
};

And I am returning this struct from a function:

struct test a()
{
    struct test t = { "yeah!", "string" };
    return t;
}

My question is whether the memory for the struct is freed automatically or if I have to do this manually via free().

[update from comment:]

The function a is in a DLL and I want to use the struct in the main program.

Community
  • 1
  • 1
Luca Schimweg
  • 747
  • 5
  • 18

3 Answers3

5

You should only free something which you malloced (or used another similar function) first. Since nothing was malloced, nothing should be freed.

SergeyA
  • 61,605
  • 5
  • 78
  • 137
  • 1
    But how long can I use the struct then? The function `a` is in a DLL and I want to use the struct in the main program. How does the compiler know when to delete the struct? – Luca Schimweg May 16 '16 at 14:53
  • Compiler uses memcopy to copy struct when you do `return t` Unfortunately DLL is doing a terrible job of using string literals ("yeah!", "string") to return data. Life-time of these is usually as long as the DLL is in memory on most systems so memory returned is mostly unusable. Better solution is to allocate on heap, do string copy & then return struct. Have caller do the free once it is done with the struct. – Chintan May 16 '16 at 14:55
  • @Chintan there is no such guarantee. A struct return value might as well be in registers or so. – Antti Haapala -- Слава Україні May 16 '16 at 14:55
  • @LucaSchimweg, this is why it is called `automatic` variable - compiler manages it's lifetime automatically for you. – SergeyA May 16 '16 at 14:56
  • I would very much like the downvoter to explain the reason for downvote. – SergeyA May 16 '16 at 14:56
  • Ok, thanks it seems as I can use the variable without any problems. – Luca Schimweg May 16 '16 at 14:59
3

TL/DR Version: You do not need to manually free anything; you can treat this struct instance the way you would treat any scalar variable.

Slightly Longer Version: The struct instance t has automatic storage duration, meaning its lifetime extends over the lifetime of the a function; once a exits, any memory allocated for t is released. A copy of the contents of t is returned to the caller.

As for those contents...

c and c2 are pointing to string literals; string literals are allocated such that their lifetime extends over the entire program's execution. So the pointer values in c and c2 will be valid after t is returned from a; indeed, those pointer values will be valid over the lifetime of the program.

You should only have to call free on something that was allocated via malloc, calloc, or realloc.

John Bode
  • 119,563
  • 19
  • 122
  • 198
0

you do not have to free a non-dynamic allocation. Nevertheless, If you want to use the struct in an other function, you have to pass the address of the struct, and take it as a (struct *), if you don't, you will not be able to use it again.

Yanis Ismail
  • 73
  • 10