First recognized byte alignment

Source: Internet
Author: User

Today, when I was writing a program, I encountered a small problem. How can I record the expression of actions on 64 devices? I thought of a resource-saving bit domain, write down the following Program (in 32-bit PC) [cpp] typedef struct {unsigned f1: 1;} new_log_t; typedef struct {unsigned f1: 1;} new_log_t; sizeof (new_log_t) = 4, doesn't it seem to save resources? Try [cpp] typedef struct {unsigned f1: 1; unsigned f2: 1; unsigned f3: 1; unsigned f4: 1; unsigned f5: 1; unsigned f6: 1; unsigned f7: 1; unsigned f8: 1;} new_log_t; typedef struct {unsigned f1: 1; unsigned f2: 1; unsigned f3: 1; unsigned f4: 1; unsigned f5: 1; unsigned f6: 1; unsigned f7: 1; unsigned f8: 1;} new_log_t; sizeof (new_log_t) = 4, what does it seem to know, try [cpp] typedef struct {unsigned f1: 1; unsigned f2: 1; unsigned f3: 1; unsigned f4: 1; unsigned f5: 1; unsigned f6: 1; unsigned f7: 1; unsigned f8: 1; unsigned f9: 1; unsigned f10: 1; unsigned f11: 1; unsigned f12: 1; unsigned f13: 1; unsigned f14: 1; unsigned f15: 1; unsigned f16: 1; unsigned f17: 1; unsigned f18: 1; unsigned f19: 1; unsigned f20: 1; unsigned f21: 1; unsigned f22: 1; unsigned f23: 1; unsigned f24: 1; unsigned f25: 1; unsigned f26: 1; unsigned f27: 1; unsigned f28: 1; unsigned f29: 1; unsigned f30: 1; unsigned f31: 1; unsigned f32: 1; unsigned f33: 1;} new_log_t; typedef struct {unsigned f1: 1; unsigned f2: 1; unsigned f3: 1; unsigned f4: 1; unsigned f5: 1; unsigned f6: 1; unsigned f7: 1; unsigned f8: 1; unsigned f9: 1; unsigned f10: 1; unsigned f11: 1; unsigned f12: 1; unsigned f13: 1; unsigned f14: 1; unsigned f15: 1; unsigned f16: 1; unsigned f17: 1; unsigned f18: 1; unsigned f19: 1; unsigned f20: 1; unsigned f21: 1; unsigned f22: 1; unsigned f23: 1; unsigned f24: 1; unsigned f25: 1; unsigned f26: 1; unsigned f27: 1; unsigned f28: 1; unsigned f29: 1; unsigned f30: 1; unsigned f31: 1; unsigned f32: 1; unsigned f33: 1;} new_log_t; sizeof (new_log_t) = 8, take a look at the total number of 32 Members. [cpp] typedef struct {unsigned f1: 1; unsigned f2: 1; unsigned f3: 1; unsigned f4: 1; unsigned f5: 1; unsigned f6: 1; unsigned f7: 1; unsigned f8: 1; unsigned f9: 1; unsigned f10: 1; unsigned f11: 1; unsigned f12: 1; unsigned f13: 1; unsigned f14: 1; unsigned f15: 1; unsigned f16: 1; unsigned f17: 1; unsigned f18: 1; unsigned f19: 1; unsigned f20: 1; unsigned f21: 1; unsigned f22: 1; unsigned f23: 1; unsigned f24: 1; unsigned f25: 1; unsigned f26: 1; unsigned f27: 1; unsigned f28: 1; unsigned f29: 1; unsigned f30: 1; unsigned f31: 1; unsigned f32: 1;} new_log_t; typedef struct {unsigned f1: 1; unsigned f2: 1; unsigned f3: 1; unsigned f4: 1; unsigned f5: 1; unsigned f6: 1; unsigned f7: 1; unsigned f8: 1; unsigned f9: 1; unsigned f10: 1; unsigned f11: 1; unsigned f12: 1; unsigned f13: 1; unsigned f14: 1; unsigned f15: 1; unsigned f16: 1; unsigned f17: 1; unsigned f18: 1; unsigned f19: 1; unsigned f20: 1; unsigned f21: 1; unsigned f22: 1; unsigned f23: 1; unsigned f24: 1; unsigned f25: 1; unsigned f26: 1; unsigned f27: 1; unsigned f28: 1; unsigned f29: 1; unsigned f30: 1; unsigned f31: 1; unsigned f32: 1;} new_log_t; sizeof (new_log_t) = 4, I understand, then [cpp] typedef struct {unsigned f1: 1; unsigned f2: 1; unsigned f3: 1; unsigned f4: 1; unsigned f5: 1; unsigned f6: 1; unsigned f7: 1; unsigned f8: 1; unsigned f9: 1; unsigned f10: 1; unsigned f11: 1; unsigned f12: 1; unsigned f13: 1; unsigned f14: 1; unsigned f15: 1; unsigned f16: 1; unsigned f17: 1; unsigned f18: 1; unsigned f19: 1; unsigned f20: 1; unsigned f21: 1; unsigned f22: 1; unsigned f23: 1; unsigned f24: 1; unsigned f25: 1; unsigned f26: 1; unsigned f27: 1; unsigned f28: 1; unsigned f29: 1; unsigned f30: 1; unsigned f31: 1; unsigned f32: 1; unsigned f33: 1; unsigned f34: 1; unsigned f35: 1; unsigned f36: 1; unsigned f37: 1; unsigned f38: 1; unsigned f39: 1; unsigned f40: 1; unsigned f41: 1; unsigned f42: 1; unsigned f43: 1; unsigned f44: 1; unsigned f45: 1; unsigned f46: 1; unsigned f47: 1; unsigned f48: 1; unsigned f49: 1; unsigned f50: 1; unsigned f51: 1; unsigned f52: 1; unsigned f53: 1; unsigned f54: 1; unsigned f55: 1; unsigned f56: 1; unsigned f57: 1; unsigned f58: 1; unsigned f59: 1; unsigned f60: 1; unsigned f61: 1; unsigned f62: 1; unsigned f63: 1; unsigned f64: 1;} new_log_t; typedef struct {unsigned f1: 1; unsigned f2: 1; unsigned f3: 1; unsigned f4: 1; unsigned f5: 1; unsigned f6: 1; unsigned f7: 1; unsigned f8: 1; unsigned f9: 1; unsigned f10: 1; unsigned f11: 1; unsigned f12: 1; unsigned f13: 1; unsigned f14: 1; unsigned f15: 1; unsigned f16: 1; unsigned f17: 1; unsigned f18: 1; unsigned f19: 1; unsigned f20: 1; unsigned f21: 1; unsigned f22: 1; unsigned f23: 1; unsigned f24: 1; unsigned f25: 1; unsigned f26: 1; unsigned f27: 1; unsigned f28: 1; unsigned f29: 1; unsigned f30: 1; unsigned f31: 1; unsigned f32: 1; unsigned f33: 1; unsigned f34: 1; unsigned f35: 1; unsigned f36: 1; unsigned f37: 1; unsigned f38: 1; unsigned f39: 1; unsigned f40: 1; unsigned f41: 1; unsigned f42: 1; unsigned f43: 1; unsigned f44: 1; unsigned f45: 1; unsigned f46: 1; unsigned f47: 1; unsigned f48: 1; unsigned f49: 1; unsigned f50: 1; unsigned f51: 1; unsigned f52: 1; unsigned f53: 1; unsigned f54: 1; unsigned f55: 1; unsigned f56: 1; unsigned f57: 1; unsigned f58: 1; unsigned f59: 1; unsigned f60: 1; unsigned f61: 1; unsigned f62: 1; unsigned f63: 1; unsigned f64: 1;} new_log_t; I guess the size of sizeof is 8 bytes, after sizeof is 8 bytes, my PC structure is 4 bytes aligned, so how can I make it one byte aligned to meet my requirements to save resources, use the following statement # pragma pack (push, 1) struct {}# pragma pack (pop) example [cpp] # pragma pack (push, 1) // The effect begins with typedef struct {unsigned f1: 1;} new_log_t; # pragma pack (pop) // The effect ends (Other struct not wrapped by this command (at least one member) sizeof is 4) # pragma pack (push, 1) // effect start typedef struct {unsigned f1: 1;} new_log_t; # pragma pack (pop) // The effect ends (the other struct (at least one member) that is not wrapped by this command is sizeof and then 4) sizeof (new_log_t) = 1, it seems that the precision can only be 1 byte. (check whether there is a better way to write it later.) Postscript: I have been listening to the byte alignment before, but I can't hear it, only when you have encountered such problems can you have a deep understanding. By looking at and understanding the needs of those who need it, you can deepen your understanding and get twice the result with half the effort.

Related Article

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.