11

I know that the size of a structure is known at compile time, so it should be possible to find the size of a structure during programming. How can I do this?

To be more specific:

I have a structure say:

struct mystruct
{
    int a;
    char b;
    float c[100];
}

I can write this line in my code and run application and see the size of this structure:

int size=sizeof(mystruct);
cout<<"size is="<<size<<endl;

But this involves adding a bit of code to my application and running it.

Is there any way that Visual Studio IDE can help me to find what is the size of this structure (for example by putting my cursor on it and pressing a key!)

MSalters
  • 173,980
  • 10
  • 155
  • 350
mans
  • 17,104
  • 45
  • 172
  • 321
  • 3
    Have you try to add `sizeof (mystruct)` in the watch windows when debugging ? – Jarod42 Jul 16 '14 at 07:56
  • @Jarod42 Thanks. I did not do that, but it is similar to the way that I am using to find the size of a struct. You need to run the application, but size is known during compilation and any static analyser of code can tell the size of struct without running the application. The main problem here is that if you can not run your application(for example when you can not compile it) you can not find the size. – mans Jul 16 '14 at 08:00
  • This is very dependent on your packing criteria. Why do you need to know the size when writing the code? Static analyzers can work it out but they don't always get it right, especially when the packing is controlled from the compilation and not from #pragma. VS allows the mixing of different packing criteria for different object files in the build. – cup Jul 16 '14 at 08:13

6 Answers6

10

Given a type T, write the following:

constexpr size_t sizeOfT = sizeof(T);

Mouse over sizeOfT, and you'll see the size of the struct as the value of this constexpr.

SongWithoutWords
  • 471
  • 5
  • 12
  • Funny, I remember that hovering "sizeof" used to work to directly see the size. It stopped working, so I used the constexpr definition and hovered the variable. Then I noticed hovering "sizeof" on the RHS worked too. So I removed the constexpr part and now just size of works again (even with other types). Maybe it forced VS to reevaluate all type sizes? – hsandt Oct 11 '21 at 11:21
7

Intellisense can tell you this. Example:

template <size_t S> class Sizer { };
int x;
Sizer<sizeof(x)> foo;

If you hover over foo, it will show 4Ui64 - the size of x is 4. The Ui64 suffix is because size_t is Unsigned, Integral and 64 bits. Since it uses Intellisense, you don't need to compile the code. You can put Sizer in your stdafx.h precompiled header.

[update] An easier to use variant, using class template argument deduction

template <typename T, size_t = sizeof(T)> struct Sizer {
    Sizer(T)
};

int x;
Sizer foo(x);
MSalters
  • 173,980
  • 10
  • 155
  • 350
  • Intellisense doesn't take packing into account. It seems to basically add up the individual size of each member variable of your struct, which can be quite wrong sometimes. – foo64 Oct 13 '15 at 23:03
  • Do you have an example? – MSalters Oct 14 '15 at 06:55
  • 1
    In VS 2012 with default packing (8 bytes): `struct X { std::function a; };` Intellisense tooltip says 24 bytes, compile time sizeof(X) == 32. I have a more complex example in my code where Intellisense says 88 bytes, sizeof(T) is actually 128. – foo64 Oct 14 '15 at 22:20
  • I think Visual Studio 2013's intellisense takes padding into account. If I force a struct to a certain alignment, 16 for x64 from 8 for x86 ,the size changes. – leetNightshade Jan 04 '16 at 20:03
  • I think this answer is quite dangerous and of limited use, because the real runtime memory representation of a struct is generated by a compiler, and IntelliSense uses it's own, different rules (which was illusatrated in previous comments to the answer). You will never be sure that you've got the correct value. – Steed Mar 17 '17 at 08:43
  • @Steed: It's been improved in recent releases, and note that VS itself isn't even consistent between debug and release builds. – MSalters Mar 17 '17 at 15:42
  • 2
    I think you are overcomplicating the issue with your struct. I discovered this by typing out `const static size_t soul = sizeof(unsigned long);` and hovering over `soul` with the mouse. However, '4Ui64' - thanks for the explanation, I didn't understand it. – coolhandle01 Jun 15 '17 at 11:29
  • I'm surprised nobody created an extension that would do this for you – BlackOverlord Dec 12 '22 at 15:50
5

Here is a useful trick, short and simple:

int a[sizeof(std::string)] = 0;

The compiler will say: cannot convert from 'int' to 'int [24]'. Here is your size 24 inside the message.

The technique uses a compiler-agnostic fact that constant-size arrays created on stack have their own type, whose signature includes the array size. But the g++ throws an "array must be initialized with a brace-enclosed initializer" message, so you must crack it with a little modified version:

int a[sizeof(std::string)];
a = 0;

error: incompatible types in assignment of ‘int’ to ‘int [32]’

(oops, the std::string size does differ between compilers)

Steed
  • 1,292
  • 1
  • 14
  • 33
  • This is neat and simple. It worked even without `= 0` - just hover mouse over the array variable. – Ajay Jun 27 '22 at 10:37
2

So to have the compiler displays the size, you may use the error message of compiler with something like:

template <size_t N> struct helper_size; // undefined.

And then put, somewhere in your code

helper_size<sizeof (mystruct)>::type h;

The error messages I got are:

gcc: error: specializing member 'helper_size<408u>::type' requires 'template<>' syntax
visual 2013: error C2027: use of undefined type 'helper_size<408>'

where you have the expected 408 value.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • There is an even simpler form of the same idea: `int a[sizeof(std::string)] = 0;`. The compiler will say `cannot convert from 'int' to 'int [24]'` – Steed Mar 17 '17 at 08:46
  • @Steed Yours is the best answer. It's short, it's simple, it just works. Would you mind making it an answer? – Andreas Haferburg Nov 15 '18 at 19:32
0

I dont think there is such an option in VS (or I'm not informed about it). Although you can calculate it on your own using this: http://msdn.microsoft.com/en-us/library/s3f49ktz.aspx

n32303
  • 831
  • 1
  • 10
  • 25
  • Thanks. I know that I can calculate it, but for complex struct and packing, the calculation is not easy. – mans Jul 16 '14 at 08:02
  • you cannot calculate it, because of padding. Char isnt guaranteed to take 1 byte of memory, because of optimizations, which depends on compiler flags – mlecz Jul 16 '14 at 08:04
0

But this involves adding a bit of code to my application .

Addition to jarod's comment:

How to: Watch an Expression in the Debugger

About debugger Expressions in Native C++

and runnning

Unfortunately other way you should apply code into your application. But in c++ 11 you can statically assert and it will be clean approach plus it is provided by language itself : static-assert-with-sizeof

Community
  • 1
  • 1
qwr
  • 3,660
  • 17
  • 29
  • Thanks, but that means I need to run my application and when I can not run it, I can not calculate my struct size easily. – mans Jul 16 '14 at 08:09
  • 1
    static assert can be helpful see this http://stackoverflow.com/questions/11526526/how-to-combine-static-assert-with-sizeof-and-stringify – qwr Jul 16 '14 at 08:10
  • static assert generate error if the size is not correct, but it can not tell you the size without starting to compiling the application. – mans Jul 16 '14 at 08:13
  • for this you should manually calculate and take some assumptions. for example in you case if we consider int 4 bytes and normal situation then it will be char(4 byte)+int(4 byte)+100*float (4)=408 (c++ 11 u can use fixed std::int32_t for int or you can write your own typedefs that deal with it) – qwr Jul 16 '14 at 08:28
  • @mans `it can not tell you the size without starting to compiling the application` So exactly when do you want to know the size of a type? While you're typing in the code in the editor? You really won't know the sizeof() a type until you compile the code. – PaulMcKenzie Jul 16 '14 at 08:38