Dynamic size of struct

Discussion in 'C++' started by ose, Jul 28, 2008.

  1. ose

    ose Guest

    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"?
    ose, Jul 28, 2008
    #1
    1. Advertising

  2. ose

    Kai-Uwe Bux Guest

    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
    Kai-Uwe Bux, Jul 28, 2008
    #2
    1. Advertising

  3. In article
    <1g7jk.269337$>,
    "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? 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 -
    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
    William Woody, Jul 31, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Fogelklou
    Replies:
    36
    Views:
    1,354
    Chris Fogelklou
    Apr 20, 2004
  2. Replies:
    5
    Views:
    519
    Flash Gordon
    Apr 9, 2006
  3. Replies:
    8
    Views:
    483
    Bob Hairgrove
    Apr 10, 2006
  4. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,529
    Michael Jung
    May 25, 2008
  5. Jaco Naude
    Replies:
    2
    Views:
    1,450
    Jaco Naude
    Jan 13, 2010
Loading...

Share This Page