Size, Alignment, and Memory Layout Insights for C++ Classes, Structs, and Unions

Mryam Girmay

In Visual Studio 2022 version 17.8, we introduced a feature that allows C++ developers to easily view the size and alignment of classes, structs, unions, base types, or Enums. Furthermore, in Visual Studio 2022 version 17.9 Preview 1, we’re taking it a step further by providing the capability to visualize the memory layout of your C++ classes, structs, and unions.  

Thanks to your feedback, you can now use these features to gain a better understanding of how data is organized and accessed in your programs. 

Size and Alignment 

Visualizing size and alignment provides C++ developers with powerful insights into the memory occupancy of their data structures and how they align in memory. Previously, users had to write additional code to calculate size and alignment for each element. Additionally, some developers utilize the /d1 MSVC compiler switches, specifically the /d1reportSingleClassLayoutAAA (where AAA is the type name) and /d1reportAllClassLayout commands. These tools are employed to gain a comprehensive understanding of the memory layout in complex programs. However, in Visual Studio 2022 version 17.8, we’ve introduced a more efficient approach. Developers can now quickly check the size and alignment inside a Quick Info tooltip by hovering over the relevant identifiers. What’s even more convenient is that for classes, structs, and unions, users can visualize size and alignment by hovering not only over their declarations but also over any occurrence where they are defined in the code. 

Image showing the Size and Alignment in Quick Info

In C++, the size of a struct or class is primarily determined by the size of its data members and any padding added for memory alignment. However, it’s essential to note that various factors, including the introduction of virtual functions, can influence this size. In the following example, we demonstrate how you can use this feature to visualize the impact of these factors, including the presence of a hidden vptr, on the size of a class.

Image displaying an example of Size and Alignment.

In this example, we have a class called SimpleClass that contains an integer ‘x’ and an object of ClassWithVirtualFunction class. However, the class with virtual function has a hidden vptr pointing to a vtable for virtual methods. This vptr imposes a high alignment requirement for the simple class, resulting in a larger size than expected. The class occupies 16 bytes, instead of the predicted 8 bytes. This increase in size is due to the 8-byte alignment that the vptr in the first class requires, which results in the compiler adding padding to the second class.

Memory Layout 

In Visual Studio 2022 version 17.9 Preview 1, we have introduced a Memory Layout view, which unveils the memory arrangement of classes, structs, and unions. This feature is particularly valuable for optimizing memory layouts. Users can now visualize padding, offsets, and sizes of all data members within their types. 

When dealing with these types, users can hover over their names, whether in their declarations or definitions. You’ll be presented with a Quick Info tooltip, which not only includes size and alignment information but also a “Memory Layout” link. Clicking this link opens a window that offers an interactive visualization of the memory organization within your types. This layout view allows you to hover over individual data members to reveal their specific offsets and sizes.  

A GIF displaying an example of Memory Layout

The feature also provides a visualization of the memory layout of the v-table for a COM type, aiding in the understanding of the fundamental structure and organization of COM objects. 

These features will greatly benefit C++ developers in improving code efficiency and comprehension, making it easier to work with memory management and data structures. Download Visual Studio now to leverage these enhancements in your C++ development process. 

This feature currently lacks full support for the ‘property’ keyword in C++/CLI. Additionally, there are issues with properties that are not rendering correctly.  

Share your feedback

We appreciate the time you’ve spent reporting issues/suggestions and hope you continue to give us feedback when using Visual Studio on what you like and what we can improve. Your feedback is critical to help us make Visual Studio the best tool it can be! You can share feedback with us via Developer Community: report any bugs or issues via report a problem and share your suggestions for new features or improvements to existing ones.   

Stay connected with the Visual Studio team by following us on YouTube, Twitter, LinkedIn, Twitch and on Microsoft Learn.  

15 comments

Comments are closed. Login to edit/delete your existing comments

  • David Lowndes 0

    Given that you can do this, would it be possible to get an IntelliSense warning/information on struct/classes that could be shrunk by re-ordering their members?

    • Mryam GirmayMicrosoft employee 0

      Thank you, David. Your idea is valuable to us. Please submit it as a ticket at our developer community. Once the ticket is submitted, our team will review it for future updates.

      • David Lowndes 0

        Consider it requested. 🙂

  • Wang Qi 1

    The feature is cool. Will it be available in Community version?

    • Mryam GirmayMicrosoft employee 0

      The ‘Size and Alignment’ feature is already included in Visual Studio 2022 version 17.8, and the ‘Memory Layout’ feature will be available in Visual Studio 2022 version 17.9.

  • OmariO 0

    Can we have that in .NET as well?

  • Todd Dworshak 1

    Very cool. Would love to see something similar for C#.

  • John Koutsoukidis 0

    This is awesome!
    I was wondering if it’s possible to invoke the memory layout window through a command, or maybe a quick action in addition to the tooltip?

    I am using Resharper, which replaces the default tooltip, but I would like to access this feature in some other way if possible 🙂

    • Mryam GirmayMicrosoft employee 0

      Thanks for your input, John! Please submit it as a ticket to our developer community for review.

    • Chris Thompson 0

      I pray yall know youre not slick, are clowns and its comical you really think you are fulling people,SAD SAD.

      CUMDUMPSTER

  • Eduard de Jong 0

    I see that people are asking this VS feature for C# and .NET as well, but I am questioning how that would be possible. The idea is quite interesting though.

    As far as my knowledge goes, .NET non primitive datatype sizes are undefined before runtime and is also platform specific. The CLR decides sizes at runtime, or at least not before JIT.
    But with the new NativeAOT form of building it may actually be hard coded in the binary (?), but that may still be for value types only. I am still not sure about that for reference types (classes), because the GC allocates and frees those dynamically.

    Also see this documentation about the operator for it:
    https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/sizeof
    There’s a good reason why it’s unsafe in C#. 🙂

    So I can see why it’s not done for C# yet.
    However, if it’s actually possible, it would of course be great. But then I also expect an to see some extra note that the displayed sizes are just estimates for the current platform only.

    For structs this might be quite useful, especially when doing native interop / PInvoke stuff.

  • John Schroedl 1

    Just wanted to say that I tried the Memory Layout addition and it’s really nice!

    A few suggestions which I think would aid usability:

    1. I would really appreciate it also showing the whole class/struct size in the .VCMemoryLayout window since I find myself switching away from the layout view just to review the tooltip for the struct to see its overall size again. I realize I could probably scroll to the last member and add values but a simple bit of text at the top would save a lot of effort.

    2. It would be nice if I could toggle between hex and decimal formatting in the offset margin.

    3. It would be nice if I could diff the layouts of classes. ex. two similar derived classes. As it is, the coloring is all different so it’s a bit tricky to visually compare. Minimally, maybe let me set the random seed used for color selection so they could be similar?

    4. Sure would be nice to Copy or export the layout as an image.

    5. I find myself wanting to right click on a member and have an option like “Go to declaration”.

    6. As you alluded, my C++/CLI code does have some issues. Not only with properties but also event declarations. Thanks for working on CLI code too!

    7. Could the document tabs be named something better than “VCMem.e8b9c38d01e5.VCMemoryLayout”? ex. “class Test Memory Layout”

    Thanks for this feature, I’m sure we’ll use it quite a bit during perf work.

  • Paul Topping 0

    Looks very nice. I hope that the Memory Layout view shows padding bytes clearly. This would be handy when minimizing the memory footprint of a class or struct. I didn’t see that here but it flashed by quickly.

    • Mryam GirmayMicrosoft employee 0

      Thanks for your insightful suggestion, Paul! Please submit your idea as a ticket through our developer community, then our dedicated team will review it for future updates.

Feedback usabilla icon