On Sun, 22 May 2022 08:35:54 GMT, Yasumasa Suenaga <ysuen...@openjdk.org> wrote:

>> `Array<T>::_data` is a pseudo flexible array member. "Pseudo" because C++
>> doesn't have flexible array members. The compiler is completely justified in
>> complaining about the apparently out-of-bounds accesses.
>> 
>> There is a "well-known" (though moderately ugly) approach to doing flexible
>> array members in C++. Something like this:
>> 
>> 
>> T* data() {
>>   return reinterpret_cast<T*>(
>>     reinterpret_cast<char*>(this) + data_offset());
>> }
>> 
>> 
>> where `data_offset()` is new and private:
>> 
>> 
>> static size_t data_offset() {
>>   return offset_of(Array, _data);
>> }
>> 
>> 
>> Use `data()` everywhere instead of using `_data` directly.
>> 
>> There are other places in HotSpot that use this kind of approach.
>
> Thanks @kimbarrett for your advice! Warnings from array.hpp have gone with 
> your suggestion.

Good.  I see you found and used the existing `base_offset_in_bytes` (which I'd 
overlooked), rather than using my suggested `data_offset`.

-------------

PR: https://git.openjdk.java.net/jdk/pull/8646

Reply via email to