Is it true that the first element always at a lower address than the last e开发者_开发百科lement,no matter whether it's in stack or heap?
Um, yes. The elements of a struct
are located in memory in the same order they're defined in the struct
definition. They may have padding between them (but there's guaranteed to be no padding before the first one, and a pointer to a struct
can be safely cast to a pointer to the first element of that struct
), but they always get laid out in memory in the same order.
Can we ask why you're asking?
Yes, you can assume that objects of a given class are all laid out the same way in memory, no matter where they are stored.
Note that the above doesn't imply anything about which end is stored at a lower address. The compiler may choose to reorder the fields of the class as it sees fit.
精彩评论