C + + programming Thought (c)--c++, focus sizeof and memory alignment

Source: Internet
Author: User

Before the theory is too much, the following is a combination of code and

1, sizeof () is a standalone operator, not a function, you can let us know any variable byte number, can incidentally learn struct,union, memory alignment

Memory alignment: For machine instructions to quickly point to the address value, the compiler internal memory is actually aligned, how to understand, with a struct as an example

Let's talk about each variable type memory size

So the struct is theoretically: 1+2+4+4+4+8+8 = 31, but actually

The actual size is 32 (1+2+1+4) + (4+4) +8+8

And then swap the int and the short position.

The actual size is 40 (1+3) +4+ (+4+) (4+4) +8+8

This way, there will be different changes in the number of system bits and compilers.

Union more unexpected

G

All of its variables share an address, so the memory size is the largest variable, only one variable value can be saved at a time, overwriting

2, the following two debugging skills

assembly or hardware embedded development, inconvenient code changes frequently, you can use

1), Pre-treatment method

#define DEBUG

#if DEBUG

/* Process Debugging */

#endif

2), code switch, add a bool parameter, its value can be entered from the function parameter

3. Function pointers

void Func () {

..................;

} This is a function declaration

void (*p) (), function pointer, pointer to a function that has no parameter return value of void type, which is the basic

There are many more complex sensations that will not be used, such as a double (* (* (*P) ()) [x]) (), p is a pointer to a function, and there is no parameter, and points to a pointer with 10 pointers to the array of function pointers-business code is seldom used, so there is basically no way to maintain it. At most, the person himself writes the internal use of the library, should not be used as an interface

NOTE: Int (* (*p ()) [10]) (); Is a function, p is a function that returns a pointer to an array containing 10 function pointers

C + + programming Thought (c)--c++, focus sizeof and memory alignment

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.