Idiosyncratic python

Ian Kelly ian.g.kelly at gmail.com
Thu Sep 24 20:19:33 CEST 2015


On Thu, Sep 24, 2015 at 12:04 PM, jmp <jeanmichel at sequans.com> wrote:
> I'm not an expert but I think this "return by value thing" is only for C++.
> In vintage C, you can only return something that fits within a register.

If that was true at one time, it was before ANSI C.

$ cat test.c
#include <assert.h>

struct foo {
  int a;
  long b;
  float c;
  double d;
};

struct foo get_foo() {
  struct foo value;
  value.a = 12;
  value.b = 92L;
  value.c = 4.5f;
  value.d = -21.5;
  return value;
}

int main() {
  struct foo value = get_foo();
  assert(value.a == 12);
  assert(value.b == 92L);
  assert(value.c == 4.5f);
  assert(value.d == -21.5);
  return 0;
}
$ gcc -Wall -O0 -std=c89 test.c
$ ./a.out
$

There is a danger however in that it's only a shallow copy. If any
struct members are pointers, then the pointer value will be copied,
not the thing pointed to.


More information about the Python-list mailing list