Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Dynamic size of struct

Reply
Thread Tools

Dynamic size of struct

 
 
ose
Guest
Posts: n/a
 
      07-27-2008
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"?


 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      07-27-2008
ose wrote:

> 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?


No.

> Is this a good, valid way of using "struct"?


Your member names are a little bit on the meaningless side. But the struct
is clearly valid and sometimes a struct like that (with better named
fields) even qualifies as good style.


Best

Kai-Uwe Bux
 
Reply With Quote
 
 
 
 
William Woody
Guest
Posts: n/a
 
      07-31-2008
In article
<1g7jk.269337$(E-Mail Removed)>,
"ose" <(E-Mail Removed)> wrote:

> 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 - http://www.velocityreviews.com/forums/(E-Mail Removed)
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
Struct size vs. Class size in memory Jaco Naude C++ 2 01-13-2010 10:59 AM
Could a struct with size 44 bytes point always points to a char array with size 2024 bytes? eagle_jyjh@citiz.net C++ 8 04-10-2006 03:05 PM
Could a struct with size 44 bytes point always points to a char array with size 2048 bytes? eagle_jyjh@citiz.net C Programming 5 04-09-2006 02:49 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments