admin管理员组

文章数量:1356761

Does struct in C++ store data sequentially in memory, especially when there are string variables?

struct stPerson {
    int Age ;
    string Phone_Number;
    string Name;
};

Does struct in C++ store data sequentially in memory, especially when there are string variables?

struct stPerson {
    int Age ;
    string Phone_Number;
    string Name;
};
Share Improve this question edited Mar 30 at 23:00 Chris 36.9k6 gold badges33 silver badges55 bronze badges asked Mar 30 at 22:45 RASRAS 295 bronze badges New contributor RAS is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 6
  • 2 Yes, the members have to be stored sequentially, and in the order they are declared. But there might be padding between members, that leaves holes. See Why isn't sizeof for a struct equal to the sum of sizeof of each member? – Some programmer dude Commented Mar 30 at 22:48
  • 1 See also e.g. stackoverflow/questions/9132502/how-are-c-strings-stored – mkrieger1 Commented Mar 30 at 22:55
  • 1 You can think of a std::string as a specialized smart pointer for strings, that also has a bunch of operations that are semantically string manipulations. The string also has a "small string optimization" where the string's data is stored within the string object itself. But that's just an implementation optimization detail. – Eljay Commented Mar 30 at 23:05
  • Unless your type is a standard layout type, pretty much most of the details are left up to the implementation. – NathanOliver Commented Mar 30 at 23:37
  • If you'd like to take a peek for yourself at how the struct is laid out in memory, you could add some temporary code like this: const char * t = static_cast<const char *>(&myStPersonStruct); for (size_t i=0; i<sizeof(struct stPerson); i++) printf(" %02x", t[i]); printf("\n"); (or use a debugger to inspect that region of memory) – Jeremy Friesner Commented Mar 31 at 0:45
 |  Show 1 more comment

1 Answer 1

Reset to default 4
  • Data members of a struct or class 1) are stored in memory in the same order as they are declared 2).

  • There can be padding between data members and at the end of the struct to account for data alignment requirements.

  • There can be extra data at the begging of the struct and in between subobjects for things like vtables for polymorphics stucts.

  • Some data that is logically part of the object can be "external" to the object memory layout: e.g. the struct can have pointers that point to memory outside of the object. This is the case of std::string. If the stored string is bigger than the small string limit (implementation defined) the actual string is dynamically allocated outside of the std::string class memory and a pointer from std::string will point to it.


1) in C++ there is no difference between class and struct other than the default access for members declared without an explicit access specifier (public:, protected, private:).

2) from SoronelHaetir:

It only applies when all data members are declared with the same protection level. If there are data members with different protection levels the compiler is allowed to clump members with the same level (although I believe they must be relatively in-order). That is, all private members will be in the declared order, but might be either before or after all protected members. And same regarding public/protected.

from cppreference Access specifiers

Member access specifiers may affect class layout: the addresses of non-static data members are only guaranteed to increase in order of declaration for the members not separated by an access specifier(until C++11)with the same access(since C++11). (until C++23)

You can read more on Layout

本文标签: How is data stored in C when using structStack Overflow