memory-alignment

Structure alignment padding, largest size of padding, and order of struct members

半腔热情 提交于 2019-12-02 10:01:48
I've been learning about structure data padding since I found out my sizeof() operator wasn't returning what I expected. According to the pattern that I've observed, it aligns structure members with the largest data type. So for example... struct MyStruct1 { char a; // 1 byte char b; // 1 byte char c; // 1 byte char d; // 1 byte char e; // 1 byte // Total 5 Bytes //Total size of struct = 5 (no padding) }; struct MyStruct2 { char a; // 1 byte char b; // 1 byte char c; // 1 byte char d; // 1 byte char e; // 1 byte short f; // 2 bytes // Total 7 Bytes //Total size of struct = 8 (1 byte of padding

Size of class with virtual function

旧城冷巷雨未停 提交于 2019-12-02 09:10:39
I was revising the C++ concepts, but I am stuck with a very simple code #include <iostream> using namespace std; class foo { public: //int i; void virtual foobar() { cout << "foobar\n"; } }; int main() { foo f; cout << sizeof(f) << endl; //cout << sizeof(f.i) << endl; return 1; } The output of the above code is 8 But when I removed comments from the code Output is 16 and 4 I did not understand when the class have no member variable present then VPTR size is 8 but after adding a variable size becomes 12. You're working on a platform where pointers are aligned to 8 bytes. Since the virtual table

Reading from an unaligned uint8_t recast as a uint32_t array - not getting all values

一世执手 提交于 2019-12-02 03:22:41
I am trying to cast a uint8_t array to uint32_t array. However, when i try to do this, I cant seem to be able to access every consecutive 4 bytes. Let us say I have a uint8_t array with 8 bytes. I would like to access byte 2 -> 6 as one uint32_t. These all get the same value *((uint32_t*)&uint8Array[0]) , *((uint32_t*)&uint8Array[1]) , *((uint32_t*)&uint8Array[2]) , *((uint32_t*)&uint8Array[3]) While *((uint32_t*)&uint8Array[4]) gets the bytes 4 -> 8 as expected. So it seem like I can not access 4 consecutive bytes from any address? Is there any way that I can do this? If you want bytes 2..6,

Writing more characters than malloced. Why does it not fail?

泄露秘密 提交于 2019-12-02 03:15:53
问题 Why does the following work and not throw some kind of segmentation fault? char *path = "/usr/bin/"; char *random = "012"; // path + random + \0 // so its malloc(13), but I get 16 bytes due to memory alignment (im on 32bit) newPath = (char *) malloc(strlen(path) + strlen(random) + 1); strcat(newPath, path); strcat(newPath, "random"); // newPath is now: "/usr/bin/012\0" which makes 13 characters. However, if I add strcat(newPath, "RANDOMBUNNIES"); shouldn't this call fail, because strcat uses

Byte Alignment for integer (or other) types in a uint8_t array

萝らか妹 提交于 2019-12-02 02:46:50
I am writing a memory manager for microcontrollers that uses a uint8_t array for the pool. From this pool, it allocates memory of the requested size to the user. I am looking at other memory implementations. Contiki has one named mmem . In their documentation they state: It must be noted that the memory allocated with mmem_alloc() is 1-byte aligned. This is different from what malloc() does. The memory allocated with malloc() is suitably aligned for every data type and the returned void pointer can be safely casted to any other pointer type. Instead, the pointer to the memory allocated by mmem

Writing more characters than malloced. Why does it not fail?

孤者浪人 提交于 2019-12-02 01:30:24
Why does the following work and not throw some kind of segmentation fault? char *path = "/usr/bin/"; char *random = "012"; // path + random + \0 // so its malloc(13), but I get 16 bytes due to memory alignment (im on 32bit) newPath = (char *) malloc(strlen(path) + strlen(random) + 1); strcat(newPath, path); strcat(newPath, "random"); // newPath is now: "/usr/bin/012\0" which makes 13 characters. However, if I add strcat(newPath, "RANDOMBUNNIES"); shouldn't this call fail, because strcat uses more memory than allocated? Consequently, shouldn't free(newPath) also fail because it tries to free 16

Is 8-byte alignment for “double” type necessary?

拥有回忆 提交于 2019-12-01 20:37:11
问题 I understand word-alignment, which makes the cpu only need to read once when reading an integer into a register. But is 8-byte alignment (let's assume 32bit system) for "double" necessary? What is the benefit? What will happen if the space for storing a "double" is just 4-byte alignment? 回答1: There are multiple hardware components that may be adversely affected by unaligned loads or stores. The interface to memory might be eight bytes wide and only able to access memory at multiples of eight

Is 8-byte alignment for “double” type necessary?

杀马特。学长 韩版系。学妹 提交于 2019-12-01 19:04:00
I understand word-alignment, which makes the cpu only need to read once when reading an integer into a register. But is 8-byte alignment (let's assume 32bit system) for "double" necessary? What is the benefit? What will happen if the space for storing a "double" is just 4-byte alignment? There are multiple hardware components that may be adversely affected by unaligned loads or stores. The interface to memory might be eight bytes wide and only able to access memory at multiples of eight bytes. Loading an unaligned eight-byte double then requires two reads on the bus. Stores are worse, because

Why does -Wcast-align not warn about cast from char* to int* on x86?

时光怂恿深爱的人放手 提交于 2019-12-01 16:28:19
I understand that gcc has an option -Wcast-align which warns whenever a pointer is cast such that the required alignment of the target is increased. Here's my program: char data[10]; int ptr = *((int *)data); On my machine, the alignment requirement of data is 1 whereas it's 8 for ptr. Why don't I get a warning? Could it be because I'm compiling it for x86? The warning will never be emitted when compiling for Linux i386 or x86-64, when using the standard ABIs for these systems. Let me explain you why that is so. First, let's see what gcc's documentation has to say about -Wcast-align : Warn

Why does -Wcast-align not warn about cast from char* to int* on x86?

拟墨画扇 提交于 2019-12-01 15:11:02
问题 I understand that gcc has an option -Wcast-align which warns whenever a pointer is cast such that the required alignment of the target is increased. Here's my program: char data[10]; int ptr = *((int *)data); On my machine, the alignment requirement of data is 1 whereas it's 8 for ptr. Why don't I get a warning? Could it be because I'm compiling it for x86? 回答1: The warning will never be emitted when compiling for Linux i386 or x86-64, when using the standard ABIs for these systems. Let me