In article
<1g*********************@bgtnsc04-news.ops.worldnet.att.net>,
"ose" <os***@hotmail.comwrote:
If a struct declared as:
struct x {int a; int b; string c;}
Since string c's content could be changed at runtime, does this mean that
"sizeof struct x" could be dynamic and changed at runtime as well? Is this a
good, valid way of using "struct"?
By 'string' I presume you mean the C++ standard string class.
Internally the std::string class maintains a pointer to a string; thus,
'string c' in your declaration above is akin to:
struct x { int a; int b; struct { overhead decls; char *ptr; } c };
Because you don't know the size of std::string because the overhead it
maintains is not defined, you cannot predict the size of struct x;
however, the overall size of struct x is fixed.
Internally variable length strings are maintained by the std::string
class by the equivalent of realloc() on ptr; thus, your struct points to
another chunk of memory that may bounce around and change size in the
heap as you manipulate the string c.
Hope this helps.
--
William Edward Woody -
wo***@alumni.caltech.edu
Chaos In Motion -
http://www.chaosinmotion.com
Freedom is the non-negotiable demand of human dignity;
the birthright of every person‹-in every civilization.
- National Security Strategy of the United States