Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C++ Internal representation of objects

Reply
Thread Tools

C++ Internal representation of objects

 
 
Arun Goel
Guest
Posts: n/a
 
      08-17-2005

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


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-17-2005
Arun Goel wrote:
> 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?


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.

> (It is not some homework assignment)..


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


 
Reply With Quote
 
 
 
 
EventHelix.com
Guest
Posts: n/a
 
      08-17-2005
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

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      08-17-2005
* Victor Bazarov:
> Arun Goel wrote:
> > 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?

>
> If by "1,2 & 3" you mean 'a', 'b' and 'c', respectively, then 'c'
> _seems_ to me the only valid answer.


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.


> I challenge you to find the
> corresponding wording in the Standard document, or at least the
> clause/subclause/paragraph or the page number.
>
> > (It is not some homework assignment)..

>
> Why not? It might as well be.


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


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


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?
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      08-17-2005
* EventHelix.com:
> The memory representation of a class is undefined in C++. Memory
> representation for structures (with public members only) is contiguous.


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?
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      08-17-2005
On 16 Aug 2005 19:55:21 -0700, "EventHelix.com" <>
wrote in comp.lang.c++:

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

> 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


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
 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      08-17-2005
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?
If the string "Jack Klein" is a contiguous part of that object,
why doesn't it show up in the sizeof s?
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-17-2005
E. Robert Tisdale wrote:
> 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?


Of course it is.

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


Who said it was [part of the object]?
 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      08-17-2005
Victor Bazarov wrote:
> E. Robert Tisdale wrote:
>
>> 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?

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

>
>
> Who said it was [part of the object]?


Are you saying that "Jack Klein" is *not* part of std::string object s?
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-17-2005
E. Robert Tisdale wrote:
> Victor Bazarov wrote:
>
>> E. Robert Tisdale wrote:
>>
>>> 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?

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

>>
>>
>>
>> Who said it was [part of the object]?

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


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

V
 
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
Exposing internal representation - security? Joe Attardi Java 13 03-21-2006 10:39 PM
struct, IEEE-754 and internal representation ej Python 4 11-09-2005 11:54 PM
Internal representation of doubles jdog1016@gmail.com C++ 7 04-22-2005 11:15 PM
"Fail to convert to internal representation" Error Benji Java 2 12-18-2003 05:58 PM
Float** internal representation (Numeric module) Alexander Sourjikov Python 4 10-22-2003 08:22 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57