Dynamic size of struct

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ose

    #1

    Dynamic size of struct

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


  • Kai-Uwe Bux

    #2
    Re: Dynamic size of struct

    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

    Comment

    • Sam

      #3
      Re: Dynamic size of struct

      ose writes:
      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?
      Of course not. Let's substitute your string with a char *:

      struct x {int a; int b; char *c; }

      sizeof(char *) is always the same, no matter how large the character string
      the pointer points to. The pointer itself has constant size. Typically, on
      32 bit machines the pointer is 4 bytes long, on 64 bit machines it's 8 bytes
      long.

      With your string, the concept is the same.
      Is this a
      good, valid way of using "struct"?
      I see nothing wrong with that.

      -----BEGIN PGP SIGNATURE-----
      Version: GnuPG v1.4.9 (GNU/Linux)

      iEYEABECAAYFAki NISYACgkQx9p3GY HlUOJVUgCfc8bFZ c8aReZM7YvdIDa0 yMcW
      WiYAnjn2RDNPlWf/myqa7iiUoWPh84i 5
      =4hL+
      -----END PGP SIGNATURE-----

      Comment

      • William Woody

        #4
        Re: Dynamic size of struct

        In article
        <1g7jk.269337$S V4.229394@bgtns c04-news.ops.worldn et.att.net>,
        "ose" <ose29@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 - woody@alumni.ca ltech.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

        Comment

        Working...