"Original" _intsizeof memory aligned with INT

Source: Internet
Author: User

#include <stdarg.h> defines the following macro

#define _INTSIZEOF (N) ((sizeof (n) + sizeof (int)-1) & ~ (sizeof (int)-1))

The primary function is to align the variable n according to the size of the int-sized memory address, returning the size of the memory-aligned N (General >=sizeof (n))

The formula is calculated as follows: it's reproduced.

for two positive integers x, n always has an integer q, R makes

x = NQ + R, where 0<= R <n//min non-negative remaining

Q, R is the only certainty. Q = [x/n], r = x-n[x/n]. This is a simple form with the remainder division. In the C language, Q, R is easy to calculate: Q = x/n, r = x% n.


The so-called X by n alignment refers to: If the r=0, take qn, if r>0, take (q+1) n. This is also equivalent to the X represented as:

x = nq + R ', where-n < R ' <=0//MAX non-positive remaining

NQ is what we ask. The key is how to calculate it in C language. Since we can handle the standard band-Rest division, we can convert the equation to a standard band-rest division and then handle it:

x+n = qn + (n+r '), where 0<n+r ' <=n//MAX positive remaining

x+n-1 = qn + (n+r '-1), where 0<= n+r '-1 <n//min non-negative remaining

so qn = [(x+n-1)/n]n. The C language is:

((x+n-1)/n) *n

if n is a power of 2, such as 2^m, then the M-bit is shifted to the left by the M-bit. So the x+n-1 of the lowest m bits clear 0. Get:

(x+n-1) & (~ (n-1))

Because it is used in va_start, the alignment of parameters in the stack should be int-aligned, so

"Original" _intsizeof memory aligned with INT

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.