C struct memory layout? - Stack Overflow


-current community-

· chat
Stack Overflow
· Meta Stack Overflow

- your communities -

Sign up or log in to customize your list.

-more stack exchange communities-

company blog
Stack Exchange Inbox Reputation and Badges
sign up log in tour help

· Tour Start here for a quick overview of the site
· Help Center Detailed answers to any questions you might have
· Meta Discuss the workings and policies of this site
· About Us Learn more about Stack Overflow the company
· Business Learn more about hiring developers or posting ads with us

Stack Overflow

· Questions
· Jobs
· Documentation
· Tags
· Users
· Badges

· Ask Question

Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your

Whether you're a beginner or an experienced developer, you /can/

Sign up and start helping → Learn more about Documentation →

** C struct memory layout? **

up vote 47 down vote favorite
I have C# background. Very newbie to low level language like C.

In C#, struct's memory laid out by compiler by default. Compiler can
re-order data fields or pad additional bits between fields implicitly. So I
had to specify some special attribute to override this behavior for exact

AFAIK, C does not reorder or align memory layout of a struct by default.
But I heard there's a little exceptions which very hard to find.

What is C's memory layout behavior? (what should be re-ordered/aligned and

c struct memory-layout

share|improve this question
edited Aug 7 '14 at 15:56
asked May 1 '10 at 5:18

add a comment | 

* 4 Answers 4 *

active oldest votes

up vote 64 down vote accepted
In C

Source: stackoverflow.com/questions/2748995/c-struct-memory-layout

how are c structs stored in memory

Data structure alignment - Wikipedia, the free encyclopedia


** Data structure alignment **

From Wikipedia, the free encyclopedia
Jump to: navigation, search

This article *needs additional citations for verification*. Please help
improve this article by adding citations to reliable sources. Unsourced
material may be challenged and removed. /(March 2009)/ /(Learn how and when
to remove this template message)/

*Data structure alignment* is the way data is arranged and accessed in
computer memory. It consists of two separate but related issues: /data
alignment/ and /data structure padding/. When a modern computer reads from
or writes to a memory address, it will do this in word sized chunks (e.g.
4 byte chunks on a 32-bit system) or larger. /Data alignment/ means
putting the data at a memory address equal to some multiple of the word
size, which increases the system's performance due to the way the CPU
handles memory. To align the data, it may be necessary to insert some
meaningless bytes between the end of the last data structure and the start
of the next, which is /data structure padding/.

For example, when the computer's word size is 4 bytes (a byte means 8 bits
on most machines, but could be different on some systems), the data to be
read should be at a memory address which is some multiple of 4. When this
is not the case, e.g. the data starts at address 14 instead of 16, then the
computer has to read two or more 4 byte chunks and do some calculation
before the requested data has been read, or it may generate an alignment
fault. Even though the previous data structure end is at address 13, the
next data structure should start at address 16. Two padding bytes are
inserted between the two data structures at addresses

Source: en.wikipedia.org/wiki/Data_structure_alignment

© 2005-2019 HaveYourSay.org