structure-packing

Memory alignment in C-structs

三世轮回 提交于 2019-12-17 04:17:38
问题 I'm working on the 32-bit machine, so I suppose that memory alignment should be 4 bytes. Say I have struct: typedef struct { unsigned short v1; unsigned short v2; unsigned short v3; } myStruct; the real size is 6 bytes, and I suppose that aligned size should be 8, but sizeof(myStruct) returns me 6. However if I write: typedef struct { unsigned short v1; unsigned short v2; unsigned short v3; int i; } myStruct; the real size is 10 bytes, aligned is 12, and this time sizeof(myStruct) == 12 . Can

Size of structure with a char, a double, an int and a t [duplicate]

萝らか妹 提交于 2019-12-03 11:06:57
问题 This question already has answers here : Why isn't sizeof for a struct equal to the sum of sizeof of each member? (11 answers) Closed 5 years ago . When I run only the code fragment int *t; std::cout << sizeof(char) << std::endl; std::cout << sizeof(double) << std::endl; std::cout << sizeof(int) << std::endl; std::cout << sizeof(t) << std::endl; it gives me a result like this: 1 8 4 4 Total: 17. But when I test sizeof struct which contains these data types it gives me 24, and I am confused.

Extra bytes when declaring a member of a struct as uint32_t

假如想象 提交于 2019-12-02 03:39:24
问题 I have a problem when using the uint32_t type from the stdint.h library. If I run the following code (on Ubuntu linux 11.10 x86_64, g++ version 4.6.1): #include "stdint.h" #include <iostream> using std::cout; typedef struct{ // api identifier uint8_t api_id; uint8_t frame_id; uint32_t dest_addr_64_h; uint32_t dest_addr_64_l; uint16_t dest_addr_16; uint8_t broadcast_radius; uint8_t options; // packet fragmentation uint16_t order_index; uint16_t total_packets; uint8_t rf_data[]; } xbee_tx_a;

Extra bytes when declaring a member of a struct as uint32_t

半城伤御伤魂 提交于 2019-12-02 01:58:08
I have a problem when using the uint32_t type from the stdint.h library. If I run the following code (on Ubuntu linux 11.10 x86_64, g++ version 4.6.1): #include "stdint.h" #include <iostream> using std::cout; typedef struct{ // api identifier uint8_t api_id; uint8_t frame_id; uint32_t dest_addr_64_h; uint32_t dest_addr_64_l; uint16_t dest_addr_16; uint8_t broadcast_radius; uint8_t options; // packet fragmentation uint16_t order_index; uint16_t total_packets; uint8_t rf_data[]; } xbee_tx_a; typedef struct{ // api identifier uint8_t api_id; uint8_t frame_id; uint16_t dest_addr_64_h; uint16_t

how size of a structure varies with different data types

此生再无相见时 提交于 2019-12-02 00:07:20
问题 I am using Linux 32 bit os, and GCC compiler. I tried with three different type of structure. in the first structure i have defined only one char variable. size of this structure is 1 that is correct. in the second structure i have defined only one int variable. here size of the structure is showing 4 that is also correct. but in the third structure when i defined one char and one int that means total size should be 5, but the output it is showing 8. Can anyone please explain how a structure

how size of a structure varies with different data types

瘦欲@ 提交于 2019-12-01 21:19:47
I am using Linux 32 bit os, and GCC compiler. I tried with three different type of structure. in the first structure i have defined only one char variable. size of this structure is 1 that is correct. in the second structure i have defined only one int variable. here size of the structure is showing 4 that is also correct. but in the third structure when i defined one char and one int that means total size should be 5, but the output it is showing 8. Can anyone please explain how a structure is assigned? typedef struct struct_size_tag { char c; //int i; }struct_size; int main() { printf("Size

size of a structure containing bit fields [duplicate]

无人久伴 提交于 2019-12-01 06:34:45
Possible Duplicate: Why isn't sizeof for a struct equal to the sum of sizeof of each member? I was trying to understand the concept of bit fields. But I am not able to find why the size of the following structure in CASE III is coming out as 8 bytes. CASE I: struct B { unsigned char c; // +8 bits } b; sizeof(b); // Output: 1 (because unsigned char takes 1 byte on my system) CASE II: struct B { unsigned b: 1; } b; sizeof(b); // Output: 4 (because unsigned takes 4 bytes on my system) CASE III: struct B { unsigned char c; // +8 bits unsigned b: 1; // +1 bit } b; sizeof(b); // Output: 8 I don't

size of a structure containing bit fields [duplicate]

回眸只為那壹抹淺笑 提交于 2019-12-01 03:26:09
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Why isn't sizeof for a struct equal to the sum of sizeof of each member? I was trying to understand the concept of bit fields. But I am not able to find why the size of the following structure in CASE III is coming out as 8 bytes. CASE I: struct B { unsigned char c; // +8 bits } b; sizeof(b); // Output: 1 (because unsigned char takes 1 byte on my system) CASE II: struct B { unsigned b: 1; } b; sizeof(b); //

size of struct in C [duplicate]

冷暖自知 提交于 2019-11-27 17:49:47
Possible Duplicate: Why isn’t sizeof for a struct equal to the sum of sizeof of each member? Consider the following C code: #include <stdio.h> struct employee { int id; char name[30]; }; int main() { struct employee e1; printf("%d %d %d", sizeof(e1.id), sizeof(e1.name), sizeof(e1)); return(0); } The output is: 4 30 36 Why is the size of the structure not equal to the sum of the sizes of its individual component variables? The compiler may add padding for alignment requirements. Note that this applies not only to padding between the fields of a struct, but also may apply to the end of the

Reinterpret struct with members of the same type as an array in a standard compliant way [duplicate]

偶尔善良 提交于 2019-11-27 07:41:41
问题 This question already has an answer here: Casting double array to a struct of doubles 6 answers In various 3d math codebases I sometimes encounter something like this: struct vec { float x, y, z; float& operator[](std::size_t i) { assert(i < 3); return (&x)[i]; } }; Which, AFAIK is illegal because implementations are allowed to spuriously add padding between members, even if they are of the same type, though none will do so in practice. Can this be made legal by imposing constraints via