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

Pages: 1-

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:20

Seems fine to me

Name: Anonymous 2009-12-25 13:21

I don't see what's wrong here.

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.

Name: Anonymous 2009-12-25 13:31

Actually there's nothing wrong with it from the sintactical point of view, but what if the Gubrush structure is something huge?

>>4
Btw, optimization cannot always be achieved. In case of non-determinism, by example:

 Guybrush fubar(int v) {
     Guybrush ret1, ret2;
 
     ret1.k = 0;
     ret1.j = 90;
 
     ret2.k = 90;
     ret2.j = 128;
 
     if (v) {
         return ret2;
     } else {
         return ret1;
     }
 }

Name: Ctard 2009-12-25 13:35

It is perfectly valid C.

Name: Anonymous 2009-12-25 14:25

>>5
    if (v) {
        return ret2;
    } else {
        return ret1;
    }

( ≖‿≖)

Name: Anonymous 2009-12-25 14:59

>>5
Incorrect, that can still be optimized.

Name: Anonymous 2009-12-25 15:22

>>5
Not a real problem, actually:

Guybrush fubar (int v)
{
    Guybrush ret;
    if (v) {
        ret.k = 0;
        ret.j = 90;
    else {
        ret.k = 90;
        ret.j = 128;
    }
    return ret;
}

Name: Anonymous 2009-12-25 15:41

>>9
Micro-optimization kiddie

Name: Anonymous 2009-12-25 15:51

>>10
Cancer that's killing software.

Name: Anonymous 2009-12-25 18:06

>>1
Yep, valid ansi C. Some ancient non-compliant compilers don't support it; I believe -Wextra, or maybe some other crazy flag, will warn about a return-by-value. Ýou really don't need to worry about those.

If the struct is huge, take a pointer instead. In my own C projects, I always take a pointer for any struct bigger than int64 as a general rule of thumb; it's very slightly more work, but if you do it everywhere it gives a very consistent API and code style (and it's fast).

Also it's obviously a basic requirement of sepples, since e.g. returning a std::string is basically returning a struct by value with the addition of calling a copy constructor (move constructor in sepplesox).

Name: Anonymous 2009-12-25 19:22

>>12
Ý
What kind of a faggot letter is that??

Name: Anonymous 2009-12-25 19:44

>>13
Ýour gay.

Name: Anonymous 2009-12-25 20:17

>>5
but what if the Gubrush structure is something huge?

Then the stack will need to be sufficiently large.

Name: Anonymous 2009-12-25 20:56

I'm a mighty pirate

Name: Anonymous 2009-12-25 21:09

A c++ pirate !

Name: Anonymous 2009-12-25 23:54

I thought that using stack-based storage as much as possible was considered better style?

Name: Anonymous 2009-12-25 23:59

>>18
What's wrong with just malloc'ing and free'ing things yourself? (except it being tedious, unlike true garbage collected languages).

Name: Anonymous 2009-12-26 0:23

>>19
Its slow, like true garbage collected languages.

Name: Anonymous 2009-12-26 1:22

>>19
Because moving the stack pointer is way faster than calling malloc and free.

Name: Anonymous 2009-12-26 2:17

>>4
might be optimized away
>>5
Btw, optimization cannot always be achieved
Please learn the difference between the following words: should, might, will, shall, would, may.

Name: HMA 2009-12-26 2:23

>>18-19
Using a pointer for the call has no implication on your storage's location.

Guybrush fag;
fubar(&fag);

Name: Anonymous 2009-12-26 6:39

>>23
Good point. But it takes two rows. By contrast:

Guybrush fag = fubar();

Name: Anonymous 2009-12-26 6:53

>>22
should, might, will, shall, would, may.
One of these words is not like the others!!

Name: Anonymous 2009-12-26 6:59

>>24
rows
( ≖‿≖)

Name: Anonymous 2009-12-26 8:16

>>25
It's may! Because I fucked her yesterday night. Oh, and you're sister too.

Name: Anonymous 2009-12-26 8:54

>>27
You sir are and idiot.
CHECKMATE!

Name: Anonymous 2009-12-26 9:04

>>28
What about his existing and idiot?

Name: Anonymous 2009-12-26 9:14

>>28
You can't play with both colors at the same time doofus

Name: OLDFAG for you 2009-12-26 9:30

>>29
This seems to be a typo, but it's a /b/ meme.

Name: Anonymous 2009-12-26 9:31

>>28
CHECKMATE MY ANUS

Name: Anonymous 2009-12-26 10:29

>>31
/facepalm

Name: Anonymous 2009-12-26 12:14

>>32
I will, along with my hot japanese girlfriend's.

Name: Anonymous 2009-12-26 18:22

>>13
Haha, I have no idea how I accomplished that. I posted that from my BlackBerry; guess I pressed an accent key?

Name: Anonymous 2009-12-26 20:11

>>34
along with my hot japanese girlfriend's.
Your hot japanese girlfriend's dick?

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