zeboidlund
zeboidlund

Reputation: 10137

Returning a struct via copy, which encapsulates a pointer

Let's say I have the following:

typedef struct a_struct_s
{
     int* dynamic_pointer;
}
a_struct;

a_struct make_struct( int length )
{
     a_struct s;
     s.dynamic_pointer = calloc( sizeof( int ), length );
     // [check here to see if s.dynamic_pointer was allocated]

     return s;
}

Since make_struct() will return a copy of the struct s, will the pointer encapsulated by it leak memory and not be able to be freed? Also, is there a performance cost with the copy itself vs. allocating s dynamically as well?

Upvotes: 0

Views: 80

Answers (4)

AndersK
AndersK

Reputation: 36102

no, its fine as you have it from that point of view however you may want to consider having another approach to allocating because returning a struct by value with a pointer to a memory block may not be so obvious that the caller needs to free a member in it.

one approach would be to make the whole struct allocated as well then have a function the free the struct

a_struct* alloc_struct( int length )
{
     a_struct s = calloc( 1, sizeof(a_struct) );
     s->dynamic_pointer = calloc( sizeof( int ), length );
     return s;
}

void free_struct( a_struct *p )
{
    if ( p != NULL )
    { 
      free( p->dynamic_pointer );
      free(p);
    }
}

Upvotes: 1

Alok Save
Alok Save

Reputation: 206566

No, as long as the caller of the function calls free() on the structure pointer member it will not leak memory.

Remember the rule:
Memory leak happens when you call malloc,calloc and never call free on the same address. As long as you do, there is no memory leak.

Note that it is very important to specify the ownership semantics when returning dynamically allocated pointers from a function. In short, your interface should explicitly mention that after the function call the dynamically allocated pointer member is owned by the caller.

Since make_struct() will return a copy of the struct s, will the pointer encapsulated by it leak memory and not be able to be freed?

Yes a copy of the structure is returned but a shallow copy.
Basically, this means that the pointer member of the copy of the structure returned points to the same memory location as the one pointed by structure pointer member of s.
Since both pointers point to the same address, as long as caller calls free on pointer member of returned structure there is no leak.

Is there a performance cost with the copy itself vs. allocating s dynamically as well?

Dynamic allocations are always costly and more prone to errors. Use them to the minimum and don't unless you really need them.

Upvotes: 1

user529758
user529758

Reputation:

Why would it? The pointer is not the allocated memory itself - if you copy the pointer, that doesn't in itself allocate memory. If you later call free() on it, it will be correctly freed. I. e., this code does not leak memory:

a_struct s = make_struct(10);
free(s.dynamic_pointer);

Upvotes: 2

sashoalm
sashoalm

Reputation: 79615

No. Even though you copy the pointer, it still points to the same place, so you can free it.

Upvotes: 2

Related Questions