Last active
June 5, 2017 20:17
-
-
Save dariomanesku/75d970c4ee3a0a37bd3e8901b24a6af3 to your computer and use it in GitHub Desktop.
Different output between GCC and MSVC. What is going on here?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This apparently is the case on VS2013 and not on VS2015. Ref: https://twitter.com/GemsMarlo/status/871543098052161537