473,397 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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
Aug 17 '05 #1
9 8961
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
Aug 17 '05 #2
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

Aug 17 '05 #3
* 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?
Aug 17 '05 #4
* 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?
Aug 17 '05 #5
On 16 Aug 2005 19:55:21 -0700, "EventHelix.com" <ev********@gmail.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
Aug 17 '05 #6
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?
Aug 17 '05 #7
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]?
Aug 17 '05 #8
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?
Aug 17 '05 #9
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
Aug 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Alexander Sourjikov | last post by:
Hi pals, i am using python for numerical simulations and noticed that when i assign a Float variable say 0.1 python sees it like 0.10000000149011612. I have also checked if python really takes...
4
by: ej | last post by:
I ran into something today I don't quite understand and I don't know all the nitty gritty details about how Python stores and handles data internally. (I think) I understand why, when you type in...
5
by: Sathyaish | last post by:
When you say char in C, it internally means "an unsigned small integer with 1-byte memory", right? More importantly, the internal representation of char does not mean "int" as in...
9
by: archana | last post by:
Hi all, I have one question regarding hashing in .net I have two string containing same data. When i see hashcode by calling gethascode, i am getting same value. I want to know how...
15
by: khan | last post by:
Hi, I read that pointer representation can non-zero bit pattern, machine specific.Compiler when comes accross value '0' in pointer context, converts it to machine specific null pointer...
1
by: DougAtHost | last post by:
In perl, how can I get the actual internal floating point data for a variable? In C, I can do something like this { float f = 5.88; unsigned char a; memcpy(a, (void *)&f,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.