Skip to content

Instantly share code, notes, and snippets.

@dariomanesku
Last active June 5, 2017 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dariomanesku/75d970c4ee3a0a37bd3e8901b24a6af3 to your computer and use it in GitHub Desktop.
Save dariomanesku/75d970c4ee3a0a37bd3e8901b24a6af3 to your computer and use it in GitHub Desktop.
Different output between GCC and MSVC. What is going on here?
#include <stdio.h>
#include <stdint.h>
#include <new>
struct Empty { }; // implicitly-defined default ctor
struct Foo : Empty
{
uint8_t a;
uint8_t b;
};
int main()
{
printf("sizeof(Empty) = %lu\n", sizeof(Empty)); // == 1 on both GCC and MSVC.
printf("sizeof(Foo) = %lu\n", sizeof(Foo)); // == 2
uint8_t buffer[128];
Foo* foo = (Foo*)buffer;
foo->a = 1;
foo->b = 2;
Empty* ee = (Empty*)foo;
ee = ::new(ee) Empty(); // Here MSVC initializes Empty with 0 and overrides foo.a !!
printf("%d %d\n", foo->a, foo->b); // Outputs '1 2' on GCC but '0 2' on MSVC.
return 0;
}
@dariomanesku
Copy link
Author

This apparently is the case on VS2013 and not on VS2015. Ref: https://twitter.com/GemsMarlo/status/871543098052161537

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment