www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - DMD .120 sizeof struct bug

reply dickl <dick221z yahoo.com> writes:
This appeared in .120 and worked in previous versions

The size of the struct should be 5 but is returned as 8.
Changing the struct to  "align(1) struct foo" gives the correct answer.

This has implications when passing structures to the Windows api.
-------------------------------------

private import std.stdio;
struct foo
{
	uint cbSize;
	char j;
}

int main()
{
	uint k=foo.sizeof; // k should be 5 but foo.sizeof returns 8
	return 0;

}
Apr 08 2005
next sibling parent Thomas Kuehne <thomas-dloop kuehne.thisisspam.cn> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

dickl schrieb am Fri, 08 Apr 2005 23:00:53 -0400:
 This appeared in .120 and worked in previous versions

 The size of the struct should be 5 but is returned as 8.
 Changing the struct to  "align(1) struct foo" gives the correct answer.

 This has implications when passing structures to the Windows api.
 -------------------------------------

 private import std.stdio;
 struct foo
 {
 	uint cbSize;
 	char j;
 }

 int main()
 {
 	uint k=foo.sizeof; // k should be 5 but foo.sizeof returns 8
 	return 0;

 }

Seems to be missing from the changelog - this was documented in the source code. I'm not sure that this solution to the GC problem is wise. Thomas -----BEGIN PGP SIGNATURE----- iD8DBQFCV3cL3w+/yD4P9tIRAvOsAKCCzog/d0dhZId82F5Ag5PnpDYuxQCaAr6x sLwhmqj64915CQYYTK2UgaM= =QtSP -----END PGP SIGNATURE-----
Apr 08 2005
prev sibling parent reply "Walter" <newshound digitalmars.com> writes:
"dickl" <dick221z yahoo.com> wrote in message
news:d37gh6$v1i$1 digitaldaemon.com...
 This appeared in .120 and worked in previous versions

 The size of the struct should be 5 but is returned as 8.
 Changing the struct to  "align(1) struct foo" gives the correct answer.

 This has implications when passing structures to the Windows api.

D was changed to match C's behavior, which gives 8 as well: #include <stdio.h> struct foo { unsigned cbSize; char j; }; void main() { printf("%d\n", sizeof(struct foo)); // prints 8 }
Apr 09 2005
next sibling parent Carlos <carlos2003nov yahoo.ca> writes:
Walter wrote:
 "dickl" <dick221z yahoo.com> wrote in message
 
 D was changed to match C's behavior, which gives 8 as well:
 

True. C:\> cl program.c C:\> program 8 and: C:\> cl -Zp1 program.c C:\> program 5 -Zp{n} should be available in D. DMD has not enough switches...
Apr 09 2005
prev sibling parent reply dickl <dick221z yahoo.com> writes:
Its fine as long as I know what it is doing and how to work around.

However, the code is from Microsoft DDK so the Microsoft compiler
is returning  5 rather than 8 and of course Windows was expecting 5.

Walter wrote:
 "dickl" <dick221z yahoo.com> wrote in message
 news:d37gh6$v1i$1 digitaldaemon.com...
 
This appeared in .120 and worked in previous versions

The size of the struct should be 5 but is returned as 8.
Changing the struct to  "align(1) struct foo" gives the correct answer.

This has implications when passing structures to the Windows api.

D was changed to match C's behavior, which gives 8 as well: #include <stdio.h> struct foo { unsigned cbSize; char j; }; void main() { printf("%d\n", sizeof(struct foo)); // prints 8 }

Apr 09 2005
parent "Walter" <newshound digitalmars.com> writes:
"dickl" <dick221z yahoo.com> wrote in message
news:d38rub$kvh$1 digitaldaemon.com...
 Its fine as long as I know what it is doing and how to work around.

 However, the code is from Microsoft DDK so the Microsoft compiler
 is returning  5 rather than 8 and of course Windows was expecting 5.

Check the DDK code - I bet there's a #pragma pack(1) around the struct declaration, which works like D's align(1).
Apr 09 2005