Questions tagged [memory-alignment]

Memory alignment means that objects and variables reside at particular offsets in the system's memory.

Anything related to memory alignment (a.k.a. data alignment) issues and techniques.

Alignment in this context means that data in memory should be stored beginning from an address that is multiple of some constant (usually a small power of 2 such as 2, 4, 8 or 16, depending on the system architecture) to avoid a performance hit when that data is accessed. For example, 32-bit processors require a 4-byte integer to reside at a memory address that is evenly divisible by 4.

See Wikipedia on memory alignment.

1055 questions
213
votes
8 answers

Purpose of memory alignment

Admittedly I don't get it. Say you have a memory with a memory word of length of 1 byte. Why can't you access a 4 byte long variable in a single memory access on an unaligned address(i.e. not divisible by 4), as it's the case with aligned addresses?
ark
  • 2,827
  • 4
  • 18
  • 18
188
votes
16 answers

Compelling examples of custom C++ allocators?

What are some really good reasons to ditch std::allocator in favor of a custom solution? Have you run across any situations where it was absolutely necessary for correctness, performance, scalability, etc? Any really clever examples? Custom…
Naaff
  • 8,961
  • 3
  • 35
  • 42
122
votes
4 answers

Why does struct alignment depend on whether a field type is primitive or user-defined?

In Noda Time v2, we're moving to nanosecond resolution. That means we can no longer use an 8-byte integer to represent the whole range of time we're interested in. That has prompted me to investigate the memory usage of the (many) structs of Noda…
Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929
100
votes
11 answers

Why can't C compilers rearrange struct members to eliminate alignment padding?

Possible Duplicate: Why doesn't GCC optimize structs? Why doesn't C++ make the structure tighter? Consider the following example on a 32 bit x86 machine: Due to alignment constraints, the following struct struct s1 { char a; int b; …
Halle Knast
  • 3,598
  • 4
  • 22
  • 32
88
votes
4 answers

Are stack variables aligned by the GCC __attribute__((aligned(x)))?

i have the following code: #include int main(void) { float a[4] __attribute__((aligned(0x1000))) = {1.0, 2.0, 3.0, 4.0}; printf("%p %p %p %p\n", &a[0], &a[1], &a[2], &a[3]); } And i have the following…
cojocar
  • 1,642
  • 2
  • 16
  • 21
85
votes
4 answers

Memory alignment : how to use alignof / alignas?

I work with shared memory right now. I can't understand alignof and alignas. cppreference is unclear : alignof returns "alignment" but what is "alignment" ? number of bytes to add for the next block to be aligned ? padded size ? Stack overflow /…
Offirmo
  • 16,196
  • 8
  • 69
  • 90
69
votes
7 answers

Aligning to cache line and knowing the cache line size

To prevent false sharing, I want to align each element of an array to a cache line. So first I need to know the size of a cache line, so I assign each element that amount of bytes. Secondly I want the start of the array to be aligned to a cache…
MetallicPriest
  • 25,675
  • 38
  • 166
  • 299
67
votes
4 answers

What is the recommended way to align memory in C++11

I am working on a single producer single consumer ring buffer implementation.I have two requirements: Align a single heap allocated instance of a ring buffer to a cache line. Align a field within a ring buffer to a cache line (to prevent false…
Rajiv
  • 2,437
  • 2
  • 16
  • 31
58
votes
7 answers

How do I organize members in a struct to waste the least space on alignment?

[Not a duplicate of Structure padding and packing. That question is about how and when padding occurs. This one is about how to deal with it.] I have just realized how much memory is wasted as a result of alignment in C++. Consider the following…
user11313931
56
votes
6 answers

What does it mean to align the stack?

I have been a high-level coder, and architectures are pretty new to me, so I decided to read the tutorial on Assembly here: http://en.wikibooks.org/wiki/X86_Assembly/Print_Version Far down the tutorial, instructions on how to convert the Hello…
Legend
  • 104,480
  • 109
  • 255
  • 385
55
votes
10 answers

Memory alignment in C-structs

I'm working on a 32-bit machine, so I suppose that the memory alignment should be 4 bytes. Say I have this struct: typedef struct { unsigned short v1; unsigned short v2; unsigned short v3; } myStruct; The plain added size is 6 bytes,…
Ivan
54
votes
6 answers

how does malloc understand alignment?

following excerpted from here pw = (widget *)malloc(sizeof(widget)); allocates raw storage. Indeed, the malloc call allocates storage that's big enough and suitably aligned to hold an object of type widget also see fast pImpl from herb…
Chang
  • 3,497
  • 2
  • 25
  • 42
53
votes
3 answers

Making std::vector allocate aligned memory

Is it possible to make std::vector of custom structs allocate aligned memory for further processing with SIMD instructions? If it is possible to do with Allocator, does anyone happen to have such an allocator he could share?
Violet Giraffe
  • 29,070
  • 38
  • 172
  • 299
49
votes
8 answers

How is a vector's data aligned?

If I want to process data in a std::vector with SSE, I need 16 byte alignment. How can I achieve that? Do I need to write my own allocator? Or does the default allocator already align to 16 byte boundaries?
fredoverflow
  • 237,063
  • 85
  • 359
  • 638
46
votes
6 answers

Do class/struct members always get created in memory in the order they were declared?

This is a question that was sparked by Rob Walker's answer here. Suppose I declare a class/struct like so: struct { char A; int B; char C; int D; }; Is it safe to assume that these members will be declared in exactly that order in…
Jason Baker
  • 171,942
  • 122
  • 354
  • 501
1
2 3
70 71