C++ Internal representation of objects

A

Arun Goel

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
 
V

Victor Bazarov

Arun said:
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
 
A

Alf P. Steinbach

* Victor Bazarov:
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.


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

Alf P. Steinbach

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

Jack Klein

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.

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

E. Robert Tisdale

Jack said:
...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?
 
V

Victor Bazarov

E. Robert Tisdale said:
Jack said:
...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]?
 
E

E. Robert Tisdale

Victor said:
E. Robert Tisdale said:
Jack said:
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?
 
V

Victor Bazarov

E. Robert Tisdale said:
Victor said:
E. Robert Tisdale said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,999
Latest member
MakersCBDGummiesReview

Latest Threads

Top