문제

The draft C++11 standard says

Unless an object is a bit-field or a base class subobject of zero size, the address of that object is the address of the first byte it occupies. Two objects that are not bit-fields may have the same address if one is a subobject of the other, or if at least one is a base class subobject of zero size and they are of different types; otherwise, they shall have distinct addresses. (1.8(6))

However, earlier it says an object can be created by a new expression, and it's conceivable that a new expression could cause a user-defined operator new() to be called that could return the address of some char[] block it's reserved for this purpose (the first allocation could return the first address of the block), which would mean there are two objects that do not have distinct addresses (the one created by new and the char[] block). Does 1.8(6) mean that it's illegal for a user-defined new to work this way? Or is it just a hole in the language definition?

도움이 되었습니까?

해결책

Clearly, if you are using a char[] to dole out addresses as your "custom" operator new, then the char[] object will overlap the object you have created.

As long as you do not use the char[] object for something else at the same time, it is not really a problem, and that is not what this section is talking about.

This is not really what this is about tho'. If you do create your own operator new, you should not allow it to return the same address (or an overlapping address) for the objects it creates space for. That would break the above rule.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top