Saturday, September 26, 2020

Digest for comp.lang.c++@googlegroups.com - 10 updates in 1 topic

Pankaj Jangid <pankaj.jangid@gmail.com>: Sep 26 01:39PM +0530

Following snippet is copied from
https://docs.microsoft.com/en-us/cpp/cpp/alignment-cpp-declarations?view=vs-2019
 
--8<---------------cut here---------------start------------->8---
// Shows the actual memory layout
struct x_
{
char a; // 1 byte
char _pad0[3]; // padding to put 'b' on 4-byte boundary
int b; // 4 bytes
short c; // 2 bytes
char d; // 1 byte
char _pad1[1]; // padding to make sizeof(x_) multiple of 4
} bar[3];
--8<---------------cut here---------------end--------------->8---
 
In the above code snippet, is my following understanding correct?
 
`char _pad0[3]` as explained is added to put `b` on 4-byte boundary. A
similar padding is not added before `short c` because it is already on
2-byte boundary (sizeof(short)). Similarly for `char d`. Each element of
the struct is given an aligned address. Right?
 
Regards
 
--
Pankaj Jangid
"Christian Hanné" <the.hanne@gmail.com>: Sep 26 10:13AM +0200

Am 26.09.2020 um 10:09 schrieb Pankaj Jangid:
> similar padding is not added before `short c` because it is already on
> 2-byte boundary (sizeof(short)). Similarly for `char d`. Each element of
> the struct is given an aligned address. Right?
 
Yes, you can rely for the same behaviour on almost any platfom.
Ian Collins <ian-news@hotmail.com>: Sep 26 09:01PM +1200

On 26/09/2020 20:09, Pankaj Jangid wrote:
> similar padding is not added before `short c` because it is already on
> 2-byte boundary (sizeof(short)). Similarly for `char d`. Each element of
> the struct is given an aligned address. Right?
 
If in doubt, add a static_assert...
 
--
Ian.
Pankaj Jangid <pankaj.jangid@gmail.com>: Sep 26 03:00PM +0530

On Sat, Sep 26 2020, Christian Hanné wrote:
> Yes, you can rely for the same behaviour on almost any platfom.
 
Thanks
 
--
Pankaj Jangid
Pankaj Jangid <pankaj.jangid@gmail.com>: Sep 26 03:03PM +0530

On Sat, Sep 26 2020, Ian Collins wrote:
 
>> is already on 2-byte boundary (sizeof(short)). Similarly for `char
>> d`. Each element of the struct is given an aligned address. Right?
 
> If in doubt, add a static_assert...
 
My question was about alignment but now I have another query.
 
`static_assert` works during compile time. First param is a constant
expression. And memory allocation is a thing of runtime. Probably this
cannot be used at all times.
 
--
Pankaj Jangid
Richard Damon <Richard@Damon-Family.org>: Sep 26 06:28AM -0400

On 9/26/20 5:33 AM, Pankaj Jangid wrote:
 
> `static_assert` works during compile time. First param is a constant
> expression. And memory allocation is a thing of runtime. Probably this
> cannot be used at all times.
 
You can write a static assert using the sizeof the structure or offsetof
a member, which are static compile time operators.
Jorgen Grahn <grahn+nntp@snipabacken.se>: Sep 26 11:18AM

On Sat, 2020-09-26, Pankaj Jangid wrote:
> char _pad1[1]; // padding to make sizeof(x_) multiple of 4
> } bar[3];
> --8<---------------cut here---------------end--------------->8---
 
Note to readers who didn't follow the link: this isn't C++ code that
Microsoft /recommends/ for any purpose; it illustrates how their
compiler would would lay out an array of {a, b, c, d} structs in
memory. It's pseudocode with C++ syntax.
 
> similar padding is not added before `short c` because it is already on
> 2-byte boundary (sizeof(short)). Similarly for `char d`. Each element of
> the struct is given an aligned address. Right?
 
Note that this is normally nothing you have to think about.
 
/Jorgen
 
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
Pankaj Jangid <pankaj.jangid@gmail.com>: Sep 26 05:06PM +0530

On Sat, Sep 26 2020, Richard Damon wrote:
 
>> cannot be used at all times.
 
> You can write a static assert using the sizeof the structure or offsetof
> a member, which are static compile time operators.
 
Yes. For my original use-case it will work.
 
Thanks.
 
--
Pankaj Jangid
"Öö Tiib" <ootiib@hot.ee>: Sep 26 06:04AM -0700

On Saturday, 26 September 2020 11:09:32 UTC+3, Pankaj Jangid wrote:
> similar padding is not added before `short c` because it is already on
> 2-byte boundary (sizeof(short)). Similarly for `char d`. Each element of
> the struct is given an aligned address. Right?
 
In my work we use those types rarely, since precise byte- and even
bit-widths tend to matter. We use usually uint8_t, int16_t, int32_t.
Where platform does not support these then the code does not compile
and so there are no need to add static_assert checking what bit
width is int. The only inconvenience with those are portable printf
format specifiers that look butt ugly.
olcott <NoOne@NoWhere.com>: Sep 26 12:10PM -0500

On 9/26/2020 6:18 AM, Jorgen Grahn wrote:
> char d; // 1 byte
> char_pad1[1]; // padding to make sizeof(x_) multiple of 4
> } bar[3];
 
 
It might make more sense to take alignment into account when defining
the struct:
 
struct x_
{
int b; // 4 bytes
short c; // 2 bytes
char a; // 1 byte
char d; // 1 byte
} bar[3];
 
--
Copyright 2020 Pete Olcott
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com.

No comments: