WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

C · Advanced · question 48 of 100

What is a memory alignment, and why is it important in C?

📕 Buy this interview preparation book: 100 C questions & answers — PDF + EPUB for $5

Memory alignment refers to the way that data is organized in memory, particularly in relation to the boundaries of memory addresses that can be accessed efficiently by the CPU. In C, memory alignment is important because it can affect the performance and correctness of programs, particularly when dealing with low-level operations such as bit manipulation or data serialization.

In C, data types have specific alignment requirements that determine how they are laid out in memory. For example, a char can be placed at any memory address, while a short typically requires a two-byte alignment and must be placed at a memory address that is a multiple of 2. Similarly, an int typically requires a four-byte alignment and must be placed at a memory address that is a multiple of 4, and so on.

When data is not properly aligned, it can result in performance penalties and even incorrect behavior on some CPU architectures. For example, if a short is not properly aligned, it may require multiple memory accesses to read or write the value, which can be slower than accessing a properly aligned short. On some architectures, accessing misaligned data can even result in a segmentation fault or other runtime error.

To ensure proper memory alignment, C compilers typically insert padding between data members in a struct or union, and may also insert padding at the end of a struct or union to ensure that it has the appropriate overall alignment. In some cases, the pragma pack directive can be used to specify a custom alignment requirement for a struct or union.

Here is an example of how memory alignment can affect the layout of data in memory:

    struct Data {
        char c;
        short s;
        int i;
    };
    
    int main() {
        printf("sizeof(Data) = %zun", sizeof(Data)); // prints "sizeof(Data) = 8"
        return 0;
    }

In this example, we define a struct called Data that contains a char, a short, and an int. Because the short requires two-byte alignment and the int requires four-byte alignment, the compiler inserts two bytes of padding between the char and the short, and two bytes of padding at the end of the struct to ensure that its overall size is a multiple of 4. As a result, the size of Data is 8 bytes, rather than the expected 7 bytes.

In general, understanding memory alignment is an important aspect of writing efficient and correct C code, particularly when working with low-level operations or data structures. By ensuring proper alignment, C programmers can avoid performance penalties and ensure that their programs behave as expected on a variety of CPU architectures.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic C interview — then scores it.
📞 Practice C — free 15 min
📕 Buy this interview preparation book: 100 C questions & answers — PDF + EPUB for $5

All 100 C questions · All topics