1,324 questions
14
votes
4
answers
1k
views
Is there an `alignof` implementation portable to standard C89 and C99?
C89 and C99 do not provide an alignof macro as part of the standard library (nor an alignof keyword as part of the language as done by C23).
C programmers found out they can define their own version ...
1
vote
1
answer
97
views
Risc-v compressed instruction alignment [closed]
When supported, compressed instructions (RVC) bring a relaxation of code address alignment from 4 to 2 bytes.
They also bring the possibility to intermix compressed and non-compressed instructions.
...
4
votes
1
answer
109
views
Is __STDCPP_DEFAULT_NEW_ALIGNMENT__ >= alignof(std::max_align_t)?
Does the C++17 standard requires
#include <cstddef>
static_assert(__STDCPP_DEFAULT_NEW_ALIGNMENT__ >= alignof(std::max_align_t));
to be true?
In other words, is it safe to assume the address ...
3
votes
0
answers
101
views
CUDA: Load misaligned float4 vector
I want to load 4 floats per thread. I know they are not 16 byte aligned. What is the best way to do this?
Specific conditions:
I cannot align the array without replicating the data because other ...
2
votes
2
answers
135
views
Is it possible to specify nested alignment for a struct and its members in C++?
I want to tightly pack a structure, but have one specific member aligned to a particular boundary. For example:
struct A {
char a;
int b;
} __attribute__((packed));
struct B {
char a;
...
0
votes
0
answers
71
views
Why must align memory address
Memory addresses must be aligned before they are used. I know that if they are not, performance costs more in CPU caching. I discovered that certain processors raise exceptions when unaligned memories ...
3
votes
4
answers
231
views
Direct assignment crashes but memcpy() works?
It's a problem I ran into in my work. Our embedded platform is a customized Android OS running on quad-core Cortex-A7.
Background: I'm receiving a set of parameters from outside my process, and it's ...
0
votes
0
answers
85
views
_aligned_malloc and implicit object creation
I'm creating a C++ allocator that should over-align the allocated memory by a runtime-determined alignment value.
According to https://en.cppreference.com/w/cpp/memory/c/aligned_alloc, the std::...
1
vote
0
answers
111
views
Writing to a NOR flash chip from a C application
I am doing bare-metal development and interfacing a microcontroller with a SPI NOR flash chip for storing NVS key-value data. My NVS key-value struct takes up 160 bytes and looks like this:
typedef ...
0
votes
2
answers
171
views
Can you read unaligned data into an aligned type? [closed]
Let's just say you a have 32-bit integer, that integer needs to be aligned in memory on a four-byte boundary. Let's just say there's a byte buffer, and you want to store the bytes of that 32-bit ...
3
votes
1
answer
203
views
Why does malloc(10) allocate 24 bytes?
I am experimenting with malloc in C to better understand how memory alignment and allocation actually work behind the scenes. Here is the simple code I am running:
#include <stdio.h>
#include &...
1
vote
1
answer
143
views
Uses of pointer arithmetic within an object
Excerpt from the book "C++ memory management" by Patrice Roy
For example, the following code is correct, albeit not something one should seek to do (it makes no
sense, and it does things in ...
-2
votes
1
answer
84
views
Why must my image be aligned to the texel block when copying from a buffer to an image?
I upload a mip chain of BC1_RGB_UNORM_BLOCK to a staging buffer in host memory. Then I do buffer to image copy. And I get the error:
Vulkan validation layer callback: vkCmdCopyBufferToImage():
...
3
votes
1
answer
101
views
PostgreSQL Alignment theory not completely accurate?
I was reading this article on the Internet regarding alignment PostgreSQL.
Based on the example of the article and on some work experience I had with the alignment, I did a small experiment with the ...
7
votes
1
answer
139
views
MSVC C4121 warning when capturing pointer-to-member in lambda due to structure alignment
I'm working on a 32-bit C++ application using MSVC with project settings /WX (treat warnings as errors) and /W4 (warning level 4). I've encountered a C4121 warning ("alignment of a member was ...
3
votes
1
answer
174
views
Alignment for vector of vectors in C++ templated type
In C++, what is the shortest way to declare a vector of vectors, where each inner std::vector's metadata fields (importantly size and pointer) have a user-chosen alignment?
Ideally I'd be able to ...
0
votes
0
answers
48
views
How to use the same name for different types in Metal and Swift while ensuring correct alignment?
I'm writing my ray tracing game engine in Swift and Metal on my Mac. So it is very necessary to make full use of the natural optimize of Metal.
My current version can work, but it is incredibly slow ...
0
votes
0
answers
131
views
Bug in `CopyAcceleratorTable` from `winuser.h` on 64-bit?
I am experiencing a weird bug in the function CopyAcceleratorTable when compiled for 64 bit (CopyAcceleratorTable).
I have two accelerator tables (from two dlls) which I want to concatenate into a ...
3
votes
2
answers
177
views
Flexible array member issues with alignment and strict aliasing
struct Node {
Node *left;
Node *right;
int height;
char data[];
};
This is how I used to define my data structure nodes, I find it very useful because I can embed the data directly in the ...
5
votes
2
answers
269
views
Can I safely use uintptr_t in my arena allocator?
I made a really simple arena allocator in C and I was wondering one thing.
Here is my header file (by the way if this api does not seems right please tell me!).
#ifndef ARENA_H
#define ARENA_H
#...
4
votes
1
answer
201
views
How to fix a warning (ignoring attributes) with a `vector` of `__m256`
I am using GCC 14.1 with C++17, and want to allocate 32 bytes-aligned memory always.
#include <immintrin.h>
#include <vector>
int main() {
std::vector<__m256> d;
}
g++ -std=c++...
3
votes
1
answer
183
views
Why does MSVC x64 C use 8-byte int32 parameter alignment instead of 4-byte?
I'm writing a C compiler as a hobby and would like it to be able to link against C static libraries produced by MSVC.
I read the Microsoft x64 ABI, and it doesn't seem to have a strongly mandated ...
2
votes
3
answers
112
views
Is it safe to cast a struct pointer to a different struct pointer having a prefix of elements?
I have this struct:
typedef struct {
int *array;
int first;
int last;
} arguments;
and I would be interested in having a different struct with some added arguments:
typedef struct {
...
1
vote
0
answers
44
views
jwasm -bin align BSS to 4
If I do:
.model tiny
.code
_start:
mov [var1], ax
.data?
ALIGN 4
var1 dw ?
var2 dw ?
buf db 4096 dup(?)
end
I get Warning A4130: Incompatible with segment alignment: 4
If ...
0
votes
1
answer
120
views
How page alignment & page borders affect performance when traversing & reading objects
I ran a test to better understand how pages affect performance when traversing & reading objects in memory but I got the opposite of what I was expecting. I'm hoping someone can explain this ...
1
vote
1
answer
457
views
Definition of `max_align_t`
I came across the following definition of max_align_t on cppreference.com:
`max_align_t is a type whose alignment requirement is at least as strict (as large) as that of every scalar type.
This ...
2
votes
1
answer
281
views
Incorrect `cast-align` warnings on GCC and Clang even with `__attribute__((aligned))`
Background
I have a struct with a flexible array member, which I am using to store arbitrarily-sized data inline (for a generic linked list for any element type), and I can simply cast data to T* to ...
0
votes
1
answer
72
views
Segmentation fault when access mapped memory
I'm writing a simple program that encrypts a file by xor'ing its bytes with a given key.
/* ... */
void encrypt_file_block(
int fd, unsigned long *file_len, unsigned long curr_file_pos,
...
2
votes
0
answers
122
views
What does memory aligning a function to 32 do?
In HTSlib, Bonfield memory aligns a series of methods to 32. Why did he do this and what does it achieve?
So, for example here there's this code:
if (type == 'c') {
if (!(q = ...
1
vote
0
answers
84
views
How the binary code space alignment influence the runtime efficiency?
I was optimizing some C language codes and realized a weird thing.
I noticed a piece of useless code (not being used by any parts) and deleted it. But the performance went down by around 4%, which is ...
0
votes
1
answer
101
views
clang misaligns stack and then tries a vmovaps in _start written as a C function
i have a simple c function in start.c
$ cat start.c
int main(int,char**);
void _start(){
char*v[2]={"k",0};
main(1,v);
}
when i compile to assembler, with clang -O -march=cannonlake -S ...
2
votes
1
answer
231
views
testing for bytemuck casting alignment issues
I have the following struct with conversion logic:
enum TagType {
DOUBLE,
BYTE,
}
impl TagType {
/// byte-size actually
fn size(&self) -> usize {
match self {
TagType::DOUBLE ...
0
votes
1
answer
77
views
Using std::align function to fix potential issue with SIGBUS due to misaligned data
I read a file, put the data into a buffer, say char array. After that I copy part of the data to a struct and increment an offset to read the next data part. The struct is like a header, whose member ...
5
votes
1
answer
174
views
Can I get the compiler to use more efficient data layout here?
The following struct is 16 bytes on my setup:
struct MyExampleStruct
{
double d;
char b;
};
Due to the rule that the size of a struct is rounded up to the minimum alignment of it's largest ...
1
vote
1
answer
74
views
Cortex-M loading 32-bit variable optimization
I'm trying to compile the following test code below, that only writes the 32-bits variable into a pointer. I write it once as byte access, and second time as word access.
void load_data_8(uint32_t ...
1
vote
0
answers
183
views
Memory alignment for heap allocated values
I've been learning about memory alignment and have some doubts.
In this stackoverflow post, there is a comment:
new and malloc, by default, align address to 8 bytes (x86) or 16 bytes
(x64), which is ...
2
votes
1
answer
103
views
What does it mean by x-byte aligned in 32 bit system?
It is said that data/instruction starting address should be a multiple of it's size and if it is so, we call it x-byte aligned where x is size of data/instruction in memory. Why it should be like this?...
0
votes
1
answer
106
views
Confusion with ALIGN 4, 3 behavior in ARM assembly
I'm currently reading "Embedded Systems with ARM Cortex-M Microcontrollers in Assembly Language and C" by Yifeng Zhu (Amazon link), and I'm stuck on a concept explained on page 88 regarding ...
2
votes
0
answers
123
views
Any performance benefits of using ALIGN 16 to align functions to a 16-Byte boundary in assembly?
I'm curious whether aligning performance-critical assembly functions to a 16-byte boundary is truly beneficial, or if it's just a micro-optimization that can be safely ignored nowadays?
For example, ...
2
votes
1
answer
62
views
How does ld decide the alignment of a section?
I crosscompile for cortex M microcontroller using arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi
The .bss has 2KB alignment
objdump:
Idx Name Size VMA LMA File off ...
-1
votes
1
answer
125
views
Why doesn't the compiler align data to the nearest 2^N bytes<= the cache line?
My limited understanding of memory alignment in C++ is that structures are typically aligned to the strictest alignment requirement of any member. This means that size of a struct is always a multiple ...
3
votes
0
answers
94
views
Reserving a block in Flash memory on PIC24FJ128GA306
I am using the PIC24FJ128GA306 microcontroller and trying to reserve a block in flash memory to store persistent data. Here is the code snippet I am using to define and allocate the persistent data:
...
0
votes
1
answer
103
views
How to Align Memory to Cache Line Using Boost Interprocess Shared Memory
I am writing a Boost.Interprocess code for shared memory between two processes. I am using managed_shared_memory. I'm initializing the memory using boost::interprocess::open_or_create, and I am ...
6
votes
3
answers
636
views
24 bit integers that are exactly 3 bytes packed
I am writing a tool to port games written in C/C++ from the 24bit eZ80 to Windows/Linux. One of the issues is that int24_t doesn't exist on x86/x64.
typedef uint32_t uint24_t doesn't always work, ...
1
vote
1
answer
134
views
How to know the size of an aligned type including padding
I have a buffer with chunks that align to the cacheline size, but have arbitrary length, let's say 100 bytes. It is clear to me that the buffer (vector) elements are aligned on cacheline borders, but ...
4
votes
0
answers
120
views
Memory alignment: inconsistency in alignas specifier in templated using declaration
I have encountered what I believe to be an inconsistency using the alignas specifier in using declarations. Where any template will mess up the alignment.
Based on the example provided in this ...
2
votes
1
answer
608
views
How to parse correctly the `.note.gnu.property` section of an ELF executable?
I am writing an ELF parser as a side project and I have encountered a problem while trying to parse .note* sections.
The documentation for ELF32 and ELF64 (available here and here, pages 42 and 13 ...
5
votes
1
answer
319
views
How to set alignment of a brand new section
in a software project of mine, on linux, I need to create a special section that needs to be populated with 40 bytes large structures. Then I would like to treat this section as an array.
For that I ...
1
vote
1
answer
233
views
When exactly is "error[E0793]: reference to packed field is unaligned" triggered?
I've been banging my head against the wall for a while on this one. The context is that I'm trying to build an embedded VM for 8-bit AVR processors, but the question is not specific to the AVR.
Here's ...
2
votes
1
answer
95
views
Influence class alignment to pack variables tightly
Suppose such structs: A is out of our control but we can change the B structure:
struct A {
int64_t x; // 8 bytes
int32_t y; // 4 bytes
// 4 padding bytes
};
struct B {
A a; ...