C++ Internal representation of objects

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

    C++ Internal representation of objects


    Hi,

    I came across this question from a website..

    According to the C++ standard, what is an object's internal representation
    in memory guaranteed to be?
    a) Initialized
    b) On a word boundary
    c) Contiguous
    d) On stack
    e) on heap

    I am not sure which is the correct option out of 1,2 & 3?

    (It is not some homework assignment)..

    Thanks in advance
    Arun


  • Victor Bazarov

    #2
    Re: C++ Internal representation of objects

    Arun Goel wrote:[color=blue]
    > I came across this question from a website..
    >
    > According to the C++ standard, what is an object's internal
    > representation in memory guaranteed to be?
    > a) Initialized
    > b) On a word boundary
    > c) Contiguous
    > d) On stack
    > e) on heap
    >
    > I am not sure which is the correct option out of 1,2 & 3?[/color]

    If by "1,2 & 3" you mean 'a', 'b' and 'c', respectively, then 'c'
    _seems_ to me the only valid answer. I challenge you to find the
    corresponding wording in the Standard document, or at least the
    clause/subclause/paragraph or the page number.
    [color=blue]
    > (It is not some homework assignment)..[/color]

    Why not? It might as well be. Of course, if I were giving such
    assignment and then checking the answer, a simple "it's 'c'" would
    not fly. You'd still have to quote the Standard for me.

    V


    Comment

    • EventHelix.com

      #3
      Re: C++ Internal representation of objects

      The memory representation of a class is undefined in C++. Memory
      representation for structures (with public members only) is contiguous.

      The following articles might help:





      --
      EventStudio 2.5 - http://www.EventHelix.com/EventStudio
      Generate Sequence diagrams from a simple declarative language

      Comment

      • Alf P. Steinbach

        #4
        Re: C++ Internal representation of objects

        * Victor Bazarov:[color=blue]
        > Arun Goel wrote:[color=green]
        > > I came across this question from a website..
        > >
        > > According to the C++ standard, what is an object's internal
        > > representation in memory guaranteed to be?
        > > a) Initialized
        > > b) On a word boundary
        > > c) Contiguous
        > > d) On stack
        > > e) on heap
        > >
        > > I am not sure which is the correct option out of 1,2 & 3?[/color]
        >
        > If by "1,2 & 3" you mean 'a', 'b' and 'c', respectively, then 'c'
        > _seems_ to me the only valid answer.[/color]

        I agree with that, but I had a _very_ long & heated discussion with David
        Abrahams (of Boost and the C++ committee) over in clc++m; he disagreed, and
        stated -- with no one contradicting that -- that his opinion was the
        intention of the committee on what the wording should express.

        The immediate interpretation problem is whether a "region" is necessarily
        contigous or not.

        I say yes, contigous, Dave said no, not necessarily.

        The semantics problem is then whether the standard's text elsewhere is or
        can be meaningful with either interpretation. I say only with "yes". Dave
        said that's not so (but I'm still not sure whether he meant only with "no").

        Given that disagreement the only correct answer, wrt. to "guaranteed ", is:

        none of the above.

        [color=blue]
        > I challenge you to find the
        > corresponding wording in the Standard document, or at least the
        > clause/subclause/paragraph or the page number.
        >[color=green]
        > > (It is not some homework assignment)..[/color]
        >
        > Why not? It might as well be.[/color]

        It's a bit advanced for a homework assignment, where you have to know all
        about things such as multiple virtual inheritance.

        [color=blue]
        > Of course, if I were giving such
        > assignment and then checking the answer, a simple "it's 'c'" would
        > not fly. You'd still have to quote the Standard for me.[/color]

        Perhaps it could marked on the basis of whether the student's circling of an
        alternative falls into the hole on the teacher's Correct Parroting Card.

        --
        A: Because it messes up the order in which people normally read text.
        Q: Why is it such a bad thing?
        A: Top-posting.
        Q: What is the most annoying thing on usenet and in e-mail?

        Comment

        • Alf P. Steinbach

          #5
          Re: C++ Internal representation of objects

          * EventHelix.com:[color=blue]
          > The memory representation of a class is undefined in C++. Memory
          > representation for structures (with public members only) is contiguous.[/color]

          A C++ 'struct' is a C++ class. A C++ 'class' is a C++ class. The only
          difference between 'struct' and 'class' is the default access.

          The memory representation of a class instance is not undefined in C++.

          The memory representation of a class instance is not currently guaranteed to
          be contigous -- although for what I regard as the most natural
          interpretation it is so in practice.

          The memory representation of a variable, on the other hand, seems to me to
          be guaranteed to be contigous, but at least one expert disagrees with that.

          Finally, public members only is not a useful criterion for anything in this
          regard.

          --
          A: Because it messes up the order in which people normally read text.
          Q: Why is it such a bad thing?
          A: Top-posting.
          Q: What is the most annoying thing on usenet and in e-mail?

          Comment

          • Jack Klein

            #6
            Re: C++ Internal representation of objects

            On 16 Aug 2005 19:55:21 -0700, "EventHelix.com " <eventhelix@gma il.com>
            wrote in comp.lang.c++:
            [color=blue]
            > The memory representation of a class is undefined in C++. Memory
            > representation for structures (with public members only) is contiguous.[/color]

            ....and memory representation for all other C++ objects, like scalars,
            pointers, floating point types, and arrays of anything, is contiguous.
            As is dynamically allocated memory.
            [color=blue]
            > The following articles might help:
            >
            > http://www.eventhelix.com/RealtimeMa...erformance.htm
            > http://www.eventhelix.com/RealtimeMa...rformance2.htm
            > http://www.eventhelix.com/RealtimeMa...ndOrdering.htm[/color]

            Remember, the C++ standard specifically defines even the lowly int as
            an object. The term "object" in C++ has nothing at all to do with
            "object oriented". It has exactly the same definition as it did in
            ISO C90.

            --
            Jack Klein
            Home: http://JK-Technology.Com
            FAQs for
            comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
            comp.lang.c++ http://www.parashift.com/c++-faq-lite/
            alt.comp.lang.l earn.c-c++

            Comment

            • E. Robert Tisdale

              #7
              Re: C++ Internal representation of objects

              Jack Klein wrote:
              [color=blue]
              > EventHelix wrote:
              >[color=green]
              >>The memory representation of a class is undefined in C++.
              >>Memory representation for structures (with public members only) is contiguous.[/color]
              >
              > ...and memory representation for all other C++ objects, like scalars,
              > pointers, floating point types, and arrays of anything, is contiguous.
              > As is dynamically allocated memory.[/color]
              [color=blue]
              > cat main.cc[/color]
              #include <iostream>
              #include <string>

              int main(int argc, char* argv[]) {

              std::string s("Jack Klein");
              std::cout << "sizeof(std::st ring) = "
              << sizeof(std::str ing) << std::endl;
              std::cout << "sizeof(s) = "
              << sizeof(s) << std::endl;

              return 0;
              }
              [color=blue]
              > g++ -Wall -ansi -pedantic -o main main.cc
              > ./main[/color]
              sizeof(std::str ing) = 4
              sizeof(s) = 4

              Is std::string s an object?
              If the string "Jack Klein" is a contiguous part of that object,
              why doesn't it show up in the sizeof s?

              Comment

              • Victor Bazarov

                #8
                Re: C++ Internal representation of objects

                E. Robert Tisdale wrote:[color=blue]
                > Jack Klein wrote:
                >[color=green]
                >> EventHelix wrote:
                >>[color=darkred]
                >>> The memory representation of a class is undefined in C++.
                >>> Memory representation for structures (with public members only) is
                >>> contiguous.[/color]
                >>
                >>
                >> ...and memory representation for all other C++ objects, like scalars,
                >> pointers, floating point types, and arrays of anything, is contiguous.
                >> As is dynamically allocated memory.[/color]
                >
                >[color=green]
                > > cat main.cc[/color]
                > #include <iostream>
                > #include <string>
                >
                > int main(int argc, char* argv[]) {
                >
                > std::string s("Jack Klein");
                > std::cout << "sizeof(std::st ring) = "
                > << sizeof(std::str ing) << std::endl;
                > std::cout << "sizeof(s) = "
                > << sizeof(s) << std::endl;
                >
                > return 0;
                > }
                >[color=green]
                > > g++ -Wall -ansi -pedantic -o main main.cc
                > > ./main[/color]
                > sizeof(std::str ing) = 4
                > sizeof(s) = 4
                >
                > Is std::string s an object?[/color]

                Of course it is.
                [color=blue]
                > If the string "Jack Klein" is a contiguous part of that object,
                > why doesn't it show up in the sizeof s?[/color]

                Who said it was [part of the object]?

                Comment

                • E. Robert Tisdale

                  #9
                  Re: C++ Internal representation of objects

                  Victor Bazarov wrote:[color=blue]
                  > E. Robert Tisdale wrote:
                  >[color=green]
                  >> Jack Klein wrote:
                  >>[color=darkred]
                  >>> EventHelix wrote:
                  >>>
                  >>>> The memory representation of a class is undefined in C++.
                  >>>> Memory representation for structures (with public members only) is
                  >>>> contiguous.
                  >>>
                  >>>
                  >>>
                  >>> ...and memory representation for all other C++ objects, like scalars,
                  >>> pointers, floating point types, and arrays of anything, is contiguous.
                  >>> As is dynamically allocated memory.[/color]
                  >>
                  >>
                  >>[color=darkred]
                  >> > cat main.cc[/color]
                  >> #include <iostream>
                  >> #include <string>
                  >>
                  >> int main(int argc, char* argv[]) {
                  >>
                  >> std::string s("Jack Klein");
                  >> std::cout << "sizeof(std::st ring) = "
                  >> << sizeof(std::str ing) << std::endl;
                  >> std::cout << "sizeof(s) = "
                  >> << sizeof(s) << std::endl;
                  >>
                  >> return 0;
                  >> }
                  >>[color=darkred]
                  >> > g++ -Wall -ansi -pedantic -o main main.cc
                  >> > ./main[/color]
                  >> sizeof(std::str ing) = 4
                  >> sizeof(s) = 4
                  >>
                  >> Is std::string s an object?[/color]
                  >
                  >
                  > Of course it is.
                  >[color=green]
                  >> If the string "Jack Klein" is a contiguous part of that object,
                  >> why doesn't it show up in the sizeof s?[/color]
                  >
                  >
                  > Who said it was [part of the object]?[/color]

                  Are you saying that "Jack Klein" is *not* part of std::string object s?

                  Comment

                  • Victor Bazarov

                    #10
                    Re: C++ Internal representation of objects

                    E. Robert Tisdale wrote:[color=blue]
                    > Victor Bazarov wrote:
                    >[color=green]
                    >> E. Robert Tisdale wrote:
                    >>[color=darkred]
                    >>> Jack Klein wrote:
                    >>>
                    >>>> EventHelix wrote:
                    >>>>
                    >>>>> The memory representation of a class is undefined in C++.
                    >>>>> Memory representation for structures (with public members only) is
                    >>>>> contiguous.
                    >>>>
                    >>>>
                    >>>>
                    >>>>
                    >>>> ...and memory representation for all other C++ objects, like scalars,
                    >>>> pointers, floating point types, and arrays of anything, is contiguous.
                    >>>> As is dynamically allocated memory.
                    >>>
                    >>>
                    >>>
                    >>>
                    >>> > cat main.cc
                    >>> #include <iostream>
                    >>> #include <string>
                    >>>
                    >>> int main(int argc, char* argv[]) {
                    >>>
                    >>> std::string s("Jack Klein");
                    >>> std::cout << "sizeof(std::st ring) = "
                    >>> << sizeof(std::str ing) << std::endl;
                    >>> std::cout << "sizeof(s) = "
                    >>> << sizeof(s) << std::endl;
                    >>>
                    >>> return 0;
                    >>> }
                    >>>
                    >>> > g++ -Wall -ansi -pedantic -o main main.cc
                    >>> > ./main
                    >>> sizeof(std::str ing) = 4
                    >>> sizeof(s) = 4
                    >>>
                    >>> Is std::string s an object?[/color]
                    >>
                    >>
                    >>
                    >> Of course it is.
                    >>[color=darkred]
                    >>> If the string "Jack Klein" is a contiguous part of that object,
                    >>> why doesn't it show up in the sizeof s?[/color]
                    >>
                    >>
                    >>
                    >> Who said it was [part of the object]?[/color]
                    >
                    >
                    > Are you saying that "Jack Klein" is *not* part of std::string object s?[/color]

                    Yes, that's what I am saying. Just like a file on disk is not part of
                    an open stream object.

                    V

                    Comment

                    Working...