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
|
Show 1 more comment
1 Answer
Reset to default 4Data 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 thestd::string
class memory and a pointer fromstd::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
版权声明:本文标题:How is data stored in C++ when using struct? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743972632a2570751.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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:05const 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