Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio sizeof() difference IntelliSense vs. compile-time

My code looks like this:

#include <stdio.h>

#pragma pack(1)
class MyClass
{
  bool a;
  bool b;
  int c;
  char d[3];
  bool e[4];
};
#pragma pack()

int main()
{
  printf("sizeof(MyClass)=%d\n", sizeof(MyClass));
  return 0;
}

The output is:

sizeof(MyClass)=13

But when I "hover" over sizeof(MyClass) I get:

enter image description here

This wouldn't have been a big issue but I'm trying to implement a compile-time assertion and it isn't working (getting a red underline):

enter image description here

Anyone have any idea how to fix this?

like image 260
rustyx Avatar asked Dec 29 '25 01:12

rustyx


1 Answers

This is not a difference between compile-time and run-time; it is a difference between your compiler and your IDE's "intellisense", the latter of which appears not to support/recognise the #pragma pack directive.

Ignore it. The size is 13.

Since you can actually build your program, you know that the compile-time assertion succeeds, despite the "red line".

It is probably worth adding a comment before that assertion, explaining that users of Visual Studio 2015 will see a false negative in their IDE for the following assertion.

You may also wish to raise a bug on Microsoft Connect, if there is not one already.

like image 154
Lightness Races in Orbit Avatar answered Dec 31 '25 13:12

Lightness Races in Orbit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!