Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

return a struct

Name: Anonymous 2009-12-25 13:14

Hi /prog/
I'm wondering if it's legal to return a struct like the div(3) function does. Example:

typedef struct {
  int k;
  int j;
} Guybrush;

Guybrush fubar() {
  Guybrush retval;
  retval.k = 42;
  retval.j = 37;
  return retval;
}


By compiling it with gcc I don't get any error nor warning. Besides the produced assembly code is strightforward: it just uses a location in the stack of the calling procedure as storage area for the "retval" variable.

My question is: is it a good way of programming? I feel it's a little weird. Also I know it's better to exploit pointers, but this is not always a choice.

Name: Anonymous 2009-12-25 13:22

>>1
Yes. It's called return by value, it involves an extra copy when calling fubar, might be optimized away by your compiler. Exploiting pointers can be done by taking in a Guybrush& in C++ or just a pointer in C and writing over it.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List