memory-alignment

Why is the “alignment” the same on 32-bit and 64-bit systems?

不羁的心 提交于 2021-02-07 04:54:29
问题 I was wondering whether the compiler would use different padding on 32-bit and 64-bit systems, so I wrote the code below in a simple VS2019 C++ console project: struct Z { char s; __int64 i; }; int main() { std::cout << sizeof(Z) <<"\n"; } What I expected on each "Platform" setting: x86: 12 X64: 16 Actual result: x86: 16 X64: 16 Since the memory word size on x86 is 4 bytes, this means it has to store the bytes of i in two different words. So I thought the compiler would do padding this way:

Allocating initialized, aligned memory

笑着哭i 提交于 2021-02-04 17:23:27
问题 I'm writing a program (in C++) in which I need to allocate arrays whose starting addresses should be aligned with the cache line size. When I allocate these arrays I also want the memory initialized to zero. Right now I have it working using the posix_memalign function. This works well for getting memory aligned arrays but the arrays are uninitilized. Is there a better function I can use to zero out the arrays when I initialize them or do I just have to settle for writing a separate loop to

Understanding stack alignment enforcement

北城余情 提交于 2021-02-04 13:57:34
问题 Consider the following C code: #include <stdint.h> void func(void) { uint32_t var = 0; return; } The unoptimized (i.e.: -O0 option) assembly code generated by GCC 4.7.2 for the code above is: func: pushl %ebp movl %esp, %ebp subl $16, %esp movl $0, -4(%ebp) nop leave ret According to the stack alignment requirements of the System V ABI , the stack must be aligned by 16 bytes before every call instruction (the stack boundary is 16 bytes by default when not changed with the option -mpreferred

MARS MIPS simulator's built-in assembler aligns more than requested?

大城市里の小女人 提交于 2021-02-01 05:14:17
问题 I have the following data segment .data a: .byte 0x11 .align 1 b: .word 0x22334455 Assuming that address "a" is 0x10010000, then the expected address for the word at b is 0x10010002, but MARS stores the word at 0x10010004, ignoring the explicit ".align" directive. By the way, I used MARS MIPS simulator (Version 4.5 on a MacBook Pro) to assemble the above code. Therefore, my question is: Is this a bug, or is it expected that the behavior of MARS differs from SGI's 1992 documentation for MIPS

MARS MIPS simulator's built-in assembler aligns more than requested?

馋奶兔 提交于 2021-02-01 05:12:18
问题 I have the following data segment .data a: .byte 0x11 .align 1 b: .word 0x22334455 Assuming that address "a" is 0x10010000, then the expected address for the word at b is 0x10010002, but MARS stores the word at 0x10010004, ignoring the explicit ".align" directive. By the way, I used MARS MIPS simulator (Version 4.5 on a MacBook Pro) to assemble the above code. Therefore, my question is: Is this a bug, or is it expected that the behavior of MARS differs from SGI's 1992 documentation for MIPS

Convert safely between uint8_t[8] & uint64_t via cast?

放肆的年华 提交于 2021-01-27 17:00:48
问题 The way I'm currently doing it (I'd prefer to get rid of the memcpy call): uint64_t integer; uint8_t string[8]; ... memcpy(&integer, &string, 8); //or swap the parameters Assuming integer array length to always be a multiple of 8 (64 bits total allocation) is a straight cast possible given compiler padding / alignment concerns? 回答1: There is absolutely no need to avoid or replace a memcpy() call if you're striving for optimization. Every modern optimizing compiler won't emit a call and

Why does new allocate 1040 extra bytes the first time?

余生长醉 提交于 2021-01-27 16:59:37
问题 I was creating this simple test program to demonstrate the way alignment works when allocating memory using standard new... #include <iostream> #include <iomanip> #include <cstdint> // // Print a reserved block: its asked size, its start address // and the size of the previous reserved block // void print(uint16_t num, uint16_t size_asked, uint8_t* p) { static uint8_t* last = nullptr; std::cout << "BLOCK " << num << ": "; std::cout << std::setfill('0') << std::setw(2) << size_asked << "b, ";

What is the difference between “minimum alignment” and “preferred alignment”?

烂漫一生 提交于 2021-01-27 06:53:32
问题 Recently I observed that on Clang 9.0 alignof and __alignof are returning different values for unsigned long long and the same has been discussed at https://reviews.llvm.org/D54814: Starting in Clang 8.0 and GCC 8.0, alignof and __alignof return different values in same cases. Specifically alignof and _Alignof return the minimum alignment for a type, where as __alignof returns the preferred alignment. I know about type alignment but never came across "minimum alignment" and "preferred

Will sizeof always be a multiple of alignof?

穿精又带淫゛_ 提交于 2020-12-30 06:51:31
问题 Is sizeof(Type) always divisible by alignof(Type) such that this statement will always be true? sizeof(Type) % alignof(Type) == 0 回答1: Yes, sizeof(Type) % alignof(Type) == 0 is true for all class types. The standard draft says: [dcl.array] ... An object of array type contains a contiguously allocated non-empty set of N subobjects of type T. [expr.sizeof] ... When applied to a class, the result is the number of bytes in an object of that class including any padding required for placing objects

Will sizeof always be a multiple of alignof?

孤人 提交于 2020-12-30 06:51:30
问题 Is sizeof(Type) always divisible by alignof(Type) such that this statement will always be true? sizeof(Type) % alignof(Type) == 0 回答1: Yes, sizeof(Type) % alignof(Type) == 0 is true for all class types. The standard draft says: [dcl.array] ... An object of array type contains a contiguously allocated non-empty set of N subobjects of type T. [expr.sizeof] ... When applied to a class, the result is the number of bytes in an object of that class including any padding required for placing objects