memory-alignment

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 memory, or is this a compiler dependent thing? I'm asking because I had always assumed...

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? ...

Safe, efficient way to access unaligned data in a network packet from C

I'm writing a program in C for Linux on an ARM9 processor. The program is to access network packets which include a sequence of tagged data like: <fieldID><length><data><fieldID><length><data> ... The fieldID and length fields are both uint16_t. The data can be 1 or more bytes (up to 64k if the full length was used, but it's not). ...

structure alignment to port code to 64-bit

Hi, I have a 32-bit .NET assembly which PInvokes into the C layer. I want to port this assembly to 64-bit. I have read many documents related to porting to 64-bit, all of which seems to suggest that we need to take care of the alignment if we are to use structures. I had a general question related to structure alignment and wanted to c...

Struct members alignment in Visual C++ 2008

Visual C++ let's you select the struct members alignemnt in the project's properties page. Problem is, this configuration is being used for all srtructs in the project. Is there any way (VC++ specific, I'd guess) to set a certain struct's member alignment individually? ...

Align native code on fixed size memory boundaries with GCC/G++/AS ?

I have a C function that contains all the code that will implement the bytecodes of a bytecode interpreter. I'm wondering if there is a way to align segments of the compiled code in memory on fixed size boundaries so that I could directly calculate the address to jump to from the value of the bytecode? Sort of the same way an array work...

Message-dispatch system in C that doesn't break strict aliasing and alignment

I'm writing an embedded control system in C that consists of multiple tasks that send messages to each other (a fairly common idiom, I believe!), but I'm having a hard time designing a mechanism which: is neat is generic is relatively efficient most importantly: is platform independent (specifically, doesn't violate strict-aliasing or ...

Memory (sbrk) 16-byte aligned shifting on pointer access

I wrote a reasonably basic memory allocator using sbrk. I ask for a chunk of memory, say 65k and carve it up as needed for variables requesting dynamic memory. I free the memory by adding it back to the 65k block. The 65k block is derived from a union sizeof(16-bytes). Then I align the block along an even 16-byte boundary. But I'm gettin...

Does unaligned memory access always cause bus errors?

According to this wiki page, bus error can be caused by unaligned memory access. The wiki page gives an example about how to trigger a bus error. In the example, we have to enable alignment checking to see the bus error. What if we disable such alignment checking? The program seems to work properly. I have a program access unaligned memo...

Cache Line Alignment (Need clarification on article)

I've recently encountered what I think is a false-sharing problem in my application, and I've looked up Sutter's article on how to align my data to cache lines. He suggests the following C++ code: // C++ (using C++0x alignment syntax) template<typename T> struct cache_line_storage { [[ align(CACHE_LINE_SIZE) ]] T data; char pad[ C...

How to ensure 16byte code alignment of Delphi routines?

Background: I have a unit of optimised Delphi/BASM routines, mostly for heavy computations. Some of these routines contain inner loops for which I can achieve a significant speed-up if the loop start is aligned to a DQWORD (16-byte) boundary. I can ensure that the loops in question are aligned as desired IF I know the alignment at the r...

SSE2 - 16-byte aligned dynamic allocation of memory

EDIT: This is a followup to SSE2 Compiler Error This is the real bug I experienced before and have reproduced below by changing the _mm_malloc statement as Michael Burr suggested: Unhandled exception at 0x00415116 in SO.exe: 0xC0000005: Access violation reading location 0xffffffff. At line label: movdqa xmm0, xmmword ptr [t1+...

C Function alignment in GCC

I am trying to byte-align a function to 16-byte boundary using the 'aligned(16)' attribute. I did the following: void __attribute__((aligned(16))) function() { } (Source: http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html) But when I compile (gcc foo.c ; no makefiles or linker scripts used), I get the following error: FOO...

How to allocate and free aligned memory in C

How do you allocate memory that's aligned to a specific boundary in C (e.g., cache line boundary)? I'm looking for malloc/free like implementation that ideally would be as portable as possible --- at least between 32 and 64 bit architectures. Edit to add: In other words, I'm looking for something that would behave like (the now obsolet...

How to ensure a member is 4-byte aligned ?

In order to use OSAtomicDecrement (mac-specific atomic operation), I need to provide a 4-byte aligned SInt32. Does this kind of cooking work ? Is there another way to deal with alignment issues ? struct SomeClass { SomeClass() { member_ = &storage_ + ((4 - (&storage_ % 4)) % 4); *member_ = 0; } SInt32 *member_; struc...

VirtualAlloc alignment

Will the memory block returned by VirtualAlloc always be aligned with the page size? In other words, will the modulus always be zero of the return value from VirtualAlloc and the page size? ...

Should boost library be dependent on structure member alignments?

I found, the hard way, that at least boost::program_options is dependent of the compiler configured structure member alignment. If you build boost using default settings and link it with a project using 4 bytes alignment (/Zp4) it will fail at runtime (made a minimal test with program_options). Boost will generate an assert indicating a...

Another Memory Alignment Question?

I understand why data need to be aligned (and all the efforts made to accomplish it like padding) so we can reduce the number of memory accesses but this assumes that processor just can fetch addresses multiples of 4(supposing we are using a 32-bit architecture). And because of that assumption we need to align memory and my question is w...

Casting a byte array to a struct pointer depends on endianness or memory alignment?

Suppose this code: unsigned char list[3] = { 1, 2, 3 }; struct _struct{ unsigned char a; unsigned char b; unsigned char c; } *s; s = ( _struct * ) list; Can I assume that always s->a == 1, s->b == 2, s->c == 3 ? Or it will depend on the system's endianness or memory alignment? ...

Accessing struct members with array subscript operator

Let have a type T and a struct having ONLY uniform elements of T type. struct Foo { T one, T two, T three }; I'd like to access them in fallowing way: struct Foo { T one, T two, T three T &operator [] (int i) { return *(T*)((size_t)this + i * cpp_offsetof(Foo, two)); } }; where cpp_offse...