C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Re: is C++ implementation allowed to store object in non-con
Goto page 1, 2, 3  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Mirek Fidler
Guest





PostPosted: Mon Jul 07, 2003 1:13 am    Post subject: Re: is C++ implementation allowed to store object in non-con Reply with quote



Quote:
There is some indication that C++ implementation is allowed to store
base class suboject of derived class in different chunk of memory.
Is
that true ? What paragraphs of standard deal with this issue ?
(closest
I have found is 3.9/5, but it does not seems to be definitive).

If eventually it is (personally, I do not think so), how is e.g.
placement operator new expected to work ?

1.8/5 only requires that a POD object occupies contiguous bytes of
storage. To me that says that a non-POD object can be split into
parts scattered around the program's memory.

How are new operators supposed to work then ? What does return
sizeof ? How are allocators supposed to work ?

Mirek


---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Bo Persson
Guest





PostPosted: Tue Jul 08, 2003 1:36 am    Post subject: Re: is C++ implementation allowed to store object in non-con Reply with quote




""Mirek Fidler"" <cxl (AT) volny (DOT) cz> skrev i meddelandet
news:be75da$20irt$1 (AT) ID-198693 (DOT) news.dfncis.de...
Quote:
There is some indication that C++ implementation is allowed to
store
base class suboject of derived class in different chunk of
memory.
Is
that true ? What paragraphs of standard deal with this issue ?
(closest
I have found is 3.9/5, but it does not seems to be definitive).

If eventually it is (personally, I do not think so), how is e.g.
placement operator new expected to work ?

1.8/5 only requires that a POD object occupies contiguous bytes of
storage. To me that says that a non-POD object can be split into
parts scattered around the program's memory.

No, not really. It says that PODs are required to store its members in
the order they are declared, just like in a C struct.

For non-POD objects, the compiler is allowed to optimize the storage
any way it wants, like storing public data at one end and private data
at the other, or storing smaller objects in the pad space between
larger members. Multiple base classes will be stored somehow, but it
is mostly up to the implementation.

Quote:

How are new operators supposed to work then ? What does return
sizeof ? How are allocators supposed to work ?

They are supposed to work anyway. The compiler writer will have to fix
that!

sizeof() reports the total size of the object. How it does that is not
specified. Storing all the data in one piece would be an obvous
solution. :-)


Bo Persson
[email]bop2 (AT) telia (DOT) com[/email]

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Ron Natalie
Guest





PostPosted: Tue Jul 08, 2003 3:04 pm    Post subject: Re: is C++ implementation allowed to store object in non-con Reply with quote




""Bo Persson"" <bop2 (AT) telia (DOT) com> wrote


Quote:
or storing smaller objects in the pad space between
larger members.

Actually, there are restrictions on what it can do. Between
access specifiers, the subsequent non-static data members
must occupy increasing memory locations.


---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Mirek Fidler
Guest





PostPosted: Tue Jul 08, 2003 6:27 pm    Post subject: Re: is C++ implementation allowed to store object in non-con Reply with quote

Quote:
How are new operators supposed to work then ? What does return
sizeof ? How are allocators supposed to work ?

They are supposed to work anyway. The compiler writer will have to fix
that!

sizeof() reports the total size of the object.

Well, let us say that compiler indeed does something strange with
base classes, like storing them elsewhere and keeping pointer to them.

Then what is the sizeof of the object ? Size of last derived class,
or sum of all non-continuos chunks ?

Mirek


---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Victor Bazarov
Guest





PostPosted: Wed Jul 09, 2003 2:43 am    Post subject: Re: is C++ implementation allowed to store object in non-con Reply with quote

"Mirek Fidler" <cxl (AT) volny (DOT) cz> wrote...
Quote:
How are new operators supposed to work then ? What does return
sizeof ? How are allocators supposed to work ?

They are supposed to work anyway. The compiler writer will have to fix
that!

sizeof() reports the total size of the object.

Well, let us say that compiler indeed does something strange with
base classes, like storing them elsewhere and keeping pointer to them.

Then what is the sizeof of the object ? Size of last derived class,
or sum of all non-continuos chunks ?

"The sizeof operator yields the number of bytes in the object
representation of its operand."

What does it matter whether the bytes are contiguous or spread around?
If you want to copy the object using memcpy or memmove you may only
do that with POD. Any other operation does not require the object to
be in contiguous memory, does it?

Victor


---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Bo-Staffan Lankinen
Guest





PostPosted: Wed Jul 09, 2003 6:41 pm    Post subject: Re: is C++ implementation allowed to store object in non-con Reply with quote

Quote:
What does it matter whether the bytes are contiguous or spread around?
If you want to copy the object using memcpy or memmove you may only
do that with POD. Any other operation does not require the object to
be in contiguous memory, does it?

I can matter when constructing with placement new.

Bo-Staffan


---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Mirek Fidler
Guest





PostPosted: Wed Jul 09, 2003 6:41 pm    Post subject: Re: is C++ implementation allowed to store object in non-con Reply with quote

Quote:
Then what is the sizeof of the object ? Size of last derived
class,
or sum of all non-continuos chunks ?

"The sizeof operator yields the number of bytes in the object
representation of its operand."

What does it matter whether the bytes are contiguous or spread around?
If you want to copy the object using memcpy or memmove you may only
do that with POD. Any other operation does not require the object to
be in contiguous memory, does it?

I thing operator new does. Custom allocators do too. That said, I
really do not understand how sizeof could return total number of bytes
of object representation, object would be splitted through the memory
and in the same time new would work while, at least, not wasting memory.
(See e.g. 5.3.4).

Mirek


---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Victor Bazarov
Guest





PostPosted: Wed Jul 09, 2003 10:02 pm    Post subject: Re: is C++ implementation allowed to store object in non-con Reply with quote

"Bo-Staffan Lankinen" <bo-staffan.lankinen (AT) gbg (DOT) bonet.se> wrote...
Quote:
What does it matter whether the bytes are contiguous or spread around?
If you want to copy the object using memcpy or memmove you may only
do that with POD. Any other operation does not require the object to
be in contiguous memory, does it?

I can matter when constructing with placement new.

I am not sure what you mean. How would that matter? Every
'new' operator works with memory address returned to it by
the allocation function (3.7.3.1), placement new is not in
any way different, it just passes extra arguments to that
allocation function. It's the job of the allocation function
to recognise that the memory is split into pieces and not to
use it, I suppose.

Victor


---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Victor Bazarov
Guest





PostPosted: Wed Jul 09, 2003 10:03 pm    Post subject: Re: is C++ implementation allowed to store object in non-con Reply with quote

"Mirek Fidler" <cxl (AT) volny (DOT) cz> wrote...
Quote:
Then what is the sizeof of the object ? Size of last derived
class,
or sum of all non-continuos chunks ?

"The sizeof operator yields the number of bytes in the object
representation of its operand."

What does it matter whether the bytes are contiguous or spread around?
If you want to copy the object using memcpy or memmove you may only
do that with POD. Any other operation does not require the object to
be in contiguous memory, does it?

I thing operator new does.

Operator new uses whatever the allocation function returns. If
the allocation function returns contiguous memory, so will be
the object.

Quote:
Custom allocators do too. That said, I
really do not understand how sizeof could return total number of bytes
of object representation, object would be splitted through the memory
and in the same time new would work while, at least, not wasting memory.
(See e.g. 5.3.4).

I can't see anything in 5.3.4 that would mandate 'new' not to
waste _any_ memory. AFAIK, many implementations actually "waste"
memory by keeping track of how the blocks were allocated and so
on.

Victor


---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
John D. Hickin
Guest





PostPosted: Thu Jul 10, 2003 3:53 am    Post subject: Re: is C++ implementation allowed to store object in non-con Reply with quote

----- Original Message -----
From: ""Mirek Fidler"" <cxl (AT) volny (DOT) cz>

Quote:
I thing operator new does. Custom allocators do too. That said, I
really do not understand how sizeof could return total number of bytes
of object representation, object would be splitted through the memory
and in the same time new would work while, at least, not wasting memory.
(See e.g. 5.3.4).


But who says that it can't waste memory? It might have to be so...

If you can still get hold of the cfront compiler (or perhaps one having a
special mode that emulates cfront's idiosyncracies) you might see that an
inheritance in the form of a kite (i.e., _diamond of death_ with a pig tail)
actually has two instances of the virtual base; the generated code only
references (in consistent fashion) one of the virtual base class instances.
Certainly a waste of space, probably a bug, but it never really caused
problems.

I do think, however, that it was the intent of the standard to:

- stipulate that objects shall occupy contiguous storage
- all instances of a given type shall have the same size and layout
- aside from holes required because of alignment requirements, allow that
wasted space could be minimized


Regards, John.


---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Bo-Staffan Lankinen
Guest





PostPosted: Thu Jul 10, 2003 7:27 am    Post subject: Re: is C++ implementation allowed to store object in non-con Reply with quote

Quote:
I am not sure what you mean. How would that matter? Every
'new' operator works with memory address returned to it by
the allocation function (3.7.3.1), placement new is not in
any way different, it just passes extra arguments to that
allocation function. It's the job of the allocation function
to recognise that the memory is split into pieces and not to
use it, I suppose.

The placement new idiom is based on the object representation being one
contiguous memory-chunk. If the address of such a memory-chunk is passed to
a placement new expression and there are multiple invokations of the
placement new operator, different parts of the object will reside in the
same memory location because the placement new operator returns the address
of the memory-chunk for all the invokations.

Bo-Staffan


---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Mirek Fidler
Guest





PostPosted: Thu Jul 10, 2003 7:27 am    Post subject: Re: is C++ implementation allowed to store object in non-con Reply with quote

Quote:
I do think, however, that it was the intent of the standard to:

- stipulate that objects shall occupy contiguous storage
- all instances of a given type shall have the same size and layout
- aside from holes required because of alignment requirements, allow
that
wasted space could be minimized

I also do think there is no other possibility, anyway purpose of my
question was to sort out what information standard gives us about it and
whether there is a direct guarantee or if another interpretation is
possible.

Mirek


---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Mirek Fidler
Guest





PostPosted: Fri Jul 11, 2003 1:36 am    Post subject: Re: is C++ implementation allowed to store object in non-con Reply with quote

Quote:
Operator new uses whatever the allocation function returns. If
the allocation function returns contiguous memory, so will be
the object.

Well, reading this, perhaps there is a confusion about what I mean
by contiguous memory. What I am asking for is whether C++ allows storage
chunks of several objects to be 'mixed' in memory, like

---|A part1|---|B part1|A part2|----|B part2|----


Also can be explained as whether "(size_t)&x - (size_t)(Base *)this"
for each member variable "x" is constant for any instance.

I am of course not interested in alignment padding or unused chunks
of memory within sizeof(T).

Mirek


---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Victor Bazarov
Guest





PostPosted: Fri Jul 11, 2003 1:38 am    Post subject: Re: is C++ implementation allowed to store object in non-con Reply with quote

"John D. Hickin" <hickin (AT) cam (DOT) org> wrote...
Quote:
----- Original Message -----
From: ""Mirek Fidler""
I thing operator new does. Custom allocators do too. That said, I
really do not understand how sizeof could return total number of bytes
of object representation, object would be splitted through the memory
and in the same time new would work while, at least, not wasting memory.
(See e.g. 5.3.4).


But who says that it can't waste memory? It might have to be so...

If you can still get hold of the cfront compiler (or perhaps one having a
special mode that emulates cfront's idiosyncracies) you might see that an
inheritance in the form of a kite (i.e., _diamond of death_ with a pig
tail)
actually has two instances of the virtual base; the generated code only
references (in consistent fashion) one of the virtual base class
instances.
Certainly a waste of space, probably a bug, but it never really caused
problems.

I do think, however, that it was the intent of the standard to:

Intent? There must have been a reason, then, why that intent
didn't make it into the Standard...

Quote:
- stipulate that objects shall occupy contiguous storage

I haven't seen a proven need for this yet. What other parts
of the Standard would require this? And please don't just say
"placement new". Please, do explain why whatever you claim to
require contiguous storage does require it.

Quote:
- all instances of a given type shall have the same size and layout
- aside from holes required because of alignment requirements, allow that
wasted space could be minimized

I think that those requirements would be unnecessarily strict.

Thank you.

Victor


---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Bo Persson
Guest





PostPosted: Fri Jul 11, 2003 4:06 pm    Post subject: Re: is C++ implementation allowed to store object in non-con Reply with quote


"James Dennett" <jdennett (AT) acm (DOT) org> skrev i meddelandet
news:YK7Pa.609$P83.92 (AT) fed1read02 (DOT) ..
Quote:
Victor Bazarov wrote:
"Bo-Staffan Lankinen" <bo-staffan.lankinen (AT) gbg (DOT) bonet.se> wrote...

What does it matter whether the bytes are contiguous or spread
around?
If you want to copy the object using memcpy or memmove you may
only
do that with POD. Any other operation does not require the
object to
be in contiguous memory, does it?

I can matter when constructing with placement new.


I am not sure what you mean. How would that matter?

struct T { ... };
unsigned char buffer[sizeof(T)];
new (static_cast<void*>(p)) T;

This must construct a T object in the buffer. It has
to assume that the buffer is contiguous, and use it.

No, the compiler can claim that unsigned char has the wrong alignment
for a T and refuse.

If you use malloc(sizeof(T)) the returned block, if any, will be
suitable. However malloc is specifically allowed to return a larger
block than requested, to make it suitably aligned.


Bo Persson
[email]bop2 (AT) telia (DOT) com[/email]

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Goto page 1, 2, 3  Next
Page 1 of 3

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.