2

What is the reason for returning a pointer to a struct, when creating an ADT in C? for example:

typedef struct some_thing st_t;

// system interface  
st_t* init_system();

.
.
some more functions
.
.
Would it be wrong to return a struct and not a pointer to a struct?

Gopi
  • 19,784
  • 4
  • 24
  • 36
Genadi
  • 249
  • 2
  • 6
  • 2
    If you returned a struct, then you'd lose the A from ADT – David Heffernan Jan 16 '15 at 17:38
  • Probably, the init function allocates memory. – mrk Jan 16 '15 at 17:45
  • There could be many reasons at once. Would be easier with non-generic example. – keltar Jan 16 '15 at 17:48
  • @saadtaame It depends on how you implement the function, you don't have to allocate, you can return a struct by value and get a copy of the struct you created in the function. I just can't think of a reason why pointer is the right way. – Genadi Jan 16 '15 at 17:53
  • 1
    @Genadi Returning a pointer also allows the caller to be oblivious to the sizeof the object. – diapir Jan 16 '15 at 17:55
  • @Genadi if this function registers resulting structure somewhere (e.g. chains it to some linked list, attaches it to dependency graph, etc.), then structure cannot be moved so easily because other parts expects it to be on that specific address. Or anything else. Possibilities are countless. – keltar Jan 16 '15 at 18:07
  • 1
    @Genadi not so much size as contents. That's the A in ADT. – David Heffernan Jan 16 '15 at 18:11

3 Answers3

3

Generally speaking, the goal here is to limit what the ABI is between a library and the application that uses it.

If the calling code only has access to a forward declaration of the struct, and only accesses that struct through functions provided by the library, then the library can change the contents of that struct without changing the ABI.

This then allows the library to be upgraded (via a shared library / dll) without needing to recompile the calling application.

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
2

If the definition of struct some_thing is not complete (that is, the struct definition isn't visible in the current translation unit), then you cannot create a variable of that type, since the compiler doesn't know how much space to set aside for it.

However, the compiler will allow you to create a pointer to that type, even if the type definition is incomplete. So that's why you usually see ADTs referenced through pointers.

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

The advantage to returning a pointer to a struct is that the contents of the struct does not have to be copied to the calling function.

Arthur Laks
  • 524
  • 4
  • 8
  • On x86_64 platforms (and ARM if I'm not mistaken), large return values are implemented as hidden pointer arguments i.e. `struct x foo(void);` is transformed to `void foo(struct x *x);`. – diapir Jan 16 '15 at 17:51
  • It's way more complicated than this. After all, how could the contents of an incomplete type be copied? – David Heffernan Jan 16 '15 at 20:01
  • @DavidHeffernan: My comment has nothing to do with opaque types, I was just pointing out that on a lot of current platforms (System V x86-64, Win64 ...) large values aren't _copied_. – diapir Jan 17 '15 at 14:32