Memory alignment summary and alignment Summary

Source: Internet
Author: User

Memory alignment summary and alignment Summary

I attended a small pre-employment training today. During the training, the teacher talked about memory alignment. I had been in touch with it before, but I didn't go deep into it. Today, the teacher spoke about it and checked the information when I came back, the following is my understanding of memory alignment.

Memory alignment can be said to be transparent to most software engineers. Memory alignment should be managed by the compiler. The C language features powerful and flexible, and allows you to operate on the memory. If you want to understand the deeper underlying things, you must have a certain understanding of memory alignment.

First, we need to align the memory:

1) platform reason: not all platforms can access any data from any address. Some hardware platforms can only access some types of data from some addresses; otherwise, an exception occurs;

2) performance problems: in order to access aligned data, the processor needs to perform two memory accesses;

Alignment rules:

We usually discuss the layout of struct data in the memory. First, the first data is placed at the position where the offset is 0, the subsequent data layout is based on the data itself and the default "alignment coefficient" of the system, or the smaller one in the "alignment coefficient" set through pre-compilation # pragma pack.

Finally, pay attention to the rounding. That is to say, the size of the memory occupied by the struct is an integer multiple of the system's default alignment coefficient or pre-compiled setting.

For example:

#include <stdio.h>#pragma pack(4)struct xx{char b;long long a;int c;char d;};#pragma pack()int main(){struct xx x1;printf("&a=%p\n",&x1.a);printf("&b=%p\n",&x1.b);printf("&a=%p\n",&x1.c);printf("&b=%p\n",&x1.d);printf("%d",sizeof(x1));return 0;}

From the result, we can see that B is first placed in the place where offset is 0, then a is long type, which occupies 8 bytes, and pre-compilation is set to 4 bytes, therefore, three bytes are reserved after B, and so on. At last, three bytes are added to meet the requirements of the round.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.