Date: Sun, 1 May 2022 11:13:14 -0400
The ‘alignof’ operator returns an object’s alignment—not it’s size.
From https://en.cppreference.com/w/cpp/language/object#Alignment:
“[alignment represents] the number of bytes between successive addresses at which objects of this type can be allocated.”
Am I missing something?
WL
> On May 1, 2022, at 6:34 AM, Jens Maurer via Std-Proposals <std-proposals_at_[hidden]> wrote:
>
> On 01/05/2022 10.22, Abdullah Qasim via Std-Proposals wrote:
>>
>>
>> Consider:
>>
>>
>>
>> class cstring {
>>
>> // assume public functions...
>>
>>
>>
>> private:
>>
>> char* ptr; // used for dynamic allocation
>>
>> uint64_t length;
>>
>> }
>>
>>
>>
>> Now, assume I make cstring c(“12”)
>>
>>
>>
>> Length takes 8 bytes.
>>
>> Allocated char array takes 3 bytes.
>>
>> Therefore:
>>
>> alignof(c)
>>
>> returns 8
>>
>>
>>
>> Why not 3?:
>>
>>
>>
>> // In cstring
>>
>> operator alignof () {
>>
>> return alignof (charArr);
>>
>> }
>>
>>
>>
>> Which is what the user expected!
>
> Are you confusing sizeof and alignof?
> Why would alignof(charArr) ever return 3?
>
> Jens
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
From https://en.cppreference.com/w/cpp/language/object#Alignment:
“[alignment represents] the number of bytes between successive addresses at which objects of this type can be allocated.”
Am I missing something?
WL
> On May 1, 2022, at 6:34 AM, Jens Maurer via Std-Proposals <std-proposals_at_[hidden]> wrote:
>
> On 01/05/2022 10.22, Abdullah Qasim via Std-Proposals wrote:
>>
>>
>> Consider:
>>
>>
>>
>> class cstring {
>>
>> // assume public functions...
>>
>>
>>
>> private:
>>
>> char* ptr; // used for dynamic allocation
>>
>> uint64_t length;
>>
>> }
>>
>>
>>
>> Now, assume I make cstring c(“12”)
>>
>>
>>
>> Length takes 8 bytes.
>>
>> Allocated char array takes 3 bytes.
>>
>> Therefore:
>>
>> alignof(c)
>>
>> returns 8
>>
>>
>>
>> Why not 3?:
>>
>>
>>
>> // In cstring
>>
>> operator alignof () {
>>
>> return alignof (charArr);
>>
>> }
>>
>>
>>
>> Which is what the user expected!
>
> Are you confusing sizeof and alignof?
> Why would alignof(charArr) ever return 3?
>
> Jens
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
Received on 2022-05-01 15:13:17