Bug 12601 – Nested structs get a context pointer even if they don't need one
Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-04-20T08:21:00Z
Last change time
2014-04-20T10:34:22Z
Assigned to
nobody
Creator
mrdarksys
Comments
Comment #0 by mrdarksys — 2014-04-20T08:21:20Z
Hi, all.
I found next unexpected behaviour: Nested functions and properties increase size of struct.
[CODE]
import std.stdio;
int main()
{
struct A
{
ushort variable;
}
struct B
{
ushort variable;
}
struct C
{
ushort variable;
@property int var()
{
return variable;
}
}
A a;
B b;
C c;
@property var(B v)
{
return v.variable;
}
writeln("A.sizeof: ", a.sizeof);
writeln("B.sizeof: ", b.sizeof);
writeln("C.sizeof: ", c.sizeof);
return 0;
}
[/CODE]
[OUTPUT]
A.sizeof: 2
B.sizeof: 2 /// property for this struct does not change size
C.sizeof: 16 /// <- real size should be 2 (has nested function-property)
[/OUTPUT]
Reproduced on:
- DMD 2.065
- GDC 4.8.2
This behaviour is founded when used next code:
[CODE]
struct FrameHeader
{
mixin(
bitfields!(
bool, "fin", 1,
bool, "rsv1", 1,
bool, "rsv2", 1,
bool, "rsv3", 1,
FrameOpcode, "opcode", 4,
bool, "mask", 1,
ubyte, "payloadLength", 7
)
);
}
long receive(void[] value)
{
/// reading is here
}
long receive(T)(ref T value) if (is(T == struct))
{
return receive((cast(void*)&value)[0..value.sizeof]); /// try read 16 bytes instead of 2.
}
// ...
FrameHeader frameHeader;
receive(frameHeader);
[/CODE]
Comment #1 by andrej.mitrovich — 2014-04-20T10:29:41Z
This is really an issue with the structs implicitly getting a context pointer when they have no reason to own one. I think this bug is filed somewhere already though.
As a workaround add 'static' to your C struct definition.
Comment #2 by andrej.mitrovich — 2014-04-20T10:34:22Z
*** This issue has been marked as a duplicate of issue 10276 ***