Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 17th, 2005, 01:25 AM
Arun Goel
Guest
 
Posts: n/a
Default 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


  #2  
Old August 17th, 2005, 01:35 AM
Victor Bazarov
Guest
 
Posts: n/a
Default 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


  #3  
Old August 17th, 2005, 04:05 AM
EventHelix.com
Guest
 
Posts: n/a
Default 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:

http://www.eventhelix.com/RealtimeMa...erformance.htm
http://www.eventhelix.com/RealtimeMa...rformance2.htm
http://www.eventhelix.com/RealtimeMa...ndOrdering.htm

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

  #4  
Old August 17th, 2005, 04:35 AM
Alf P. Steinbach
Guest
 
Posts: n/a
Default 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?
  #5  
Old August 17th, 2005, 04:45 AM
Jack Klein
Guest
 
Posts: n/a
Default Re: C++ Internal representation of objects

On 16 Aug 2005 19:55:21 -0700, "EventHelix.com" <eventhelix@gmail.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.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
  #6  
Old August 17th, 2005, 04:45 AM
Alf P. Steinbach
Guest
 
Posts: n/a
Default 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?
  #7  
Old August 17th, 2005, 05:05 PM
E. Robert Tisdale
Guest
 
Posts: n/a
Default 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::string) = "
<< sizeof(std::string) << 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::string) = 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?
  #8  
Old August 17th, 2005, 05:25 PM
Victor Bazarov
Guest
 
Posts: n/a
Default 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::string) = "
> << sizeof(std::string) << 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::string) = 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]?
  #9  
Old August 17th, 2005, 06:05 PM
E. Robert Tisdale
Guest
 
Posts: n/a
Default 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::string) = "
>> << sizeof(std::string) << 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::string) = 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?
  #10  
Old August 17th, 2005, 07:15 PM
Victor Bazarov
Guest
 
Posts: n/a
Default 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::string) = "
>>> << sizeof(std::string) << std::endl;
>>> std::cout << "sizeof(s) = "
>>> << sizeof(s) << std::endl;
>>>
>>> return 0;
>>> }
>>>
>>> > g++ -Wall -ansi -pedantic -o main main.cc
>>> > ./main
>>> sizeof(std::string) = 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
 

Bookmarks

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles