D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 8810 - struct not returned properly from extern (C) functions
Summary: struct not returned properly from extern (C) functions
Status: RESOLVED DUPLICATE of issue 5570
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 Linux
: P2 major
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-10-12 23:09 UTC by Adrian Matoga
Modified: 2012-10-12 23:32 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Adrian Matoga 2012-10-12 23:09:41 UTC
// foo.c

typedef struct {
	unsigned bar;
	unsigned baz;
} Foo;

Foo setFoo(unsigned a, unsigned b) {
	Foo foo;
	foo.bar = a;
	foo.baz = b;
}

void setFooByPointer(unsigned a, unsigned b, Foo *pFoo) {
	pFoo->bar = a;
	pFoo->baz = b;
}

// strukt.d

import std.stdio;

struct Foo {
	uint bar;
	uint baz;
}

extern (C) Foo setFoo(uint a, uint b);
extern (C) void setFooByPointer(uint a, uint b, Foo* pFoo);

void main() {
	Foo foo;
	setFooByPointer(31337, 42, &foo);
	writeln(foo);
	foo = setFoo(31337, 42);
	writeln(foo);
}

// build

$ dmd -c strukt.d -ofstrukt.o
$ gcc -c foo.c -o foo.o
$ dmd foo.o strukt.o -ofstrukt

// run

$ ./strukt 
Foo(31337, 42)
Foo(42, 0)

// expected:
Foo(31337, 42)
Foo(31337, 42)
Comment 1 Maxim Fomin 2012-10-12 23:25:40 UTC
Looks like issue 5570
Comment 2 Adrian Matoga 2012-10-12 23:32:04 UTC
Indeed. Sorry, search didn't point me to that one.

*** This issue has been marked as a duplicate of issue 5570 ***