_INTSIZEOF(n)

G

Guest

Following macro is found inside Microsoft C++ source code:

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

The result between _INTSIZEOF(n) and sizeof(n) are always the same. I'm not sure why they defined this one. Specifically, what's the purpose of minus one for ?

Anybody has any ideas?

Thanks
 
S

Stoyan Damov

The code aligns the number "n" to the nearest multiple of sizeof(int), i.e.
4 on a 32-bit OS.
For example: _INTSIZEOF(3) = 4, _INTSIZEOF(4) = 4, _INTSIZEOF(7) = 8, etc.
etc.

HTH,
Stoyan Damov

kingdomzhf said:
Following macro is found inside Microsoft C++ source code:

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

The result between _INTSIZEOF(n) and sizeof(n) are always the same. I'm
not sure why they defined this one. Specifically, what's the purpose of
minus one for ?
 
A

Andreas Busse

Hi,

kingdomzhf said:
Following macro is found inside Microsoft C++ source code:
#define _INTSIZEOF(n) ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )
The result between _INTSIZEOF(n) and sizeof(n) are always the same. I'm
not sure why they defined this one. Specifically, what's the purpose of
minus one for ?
The macro aligns arbitrary memory block sizes to their 4-byte-block
equivalents. That means every block size that is not a multiple of four is
rounded up towards the next multiple of four. This is done by incrementing
the current block size by 3 (first part of the macro) and cutting off the
last 2 bits which equals a modulo-4 operation (second part of the macro).
Hence, the macro's output for a char[3], char[6], char[11] should be 4, 8,
12, rather than sizeof(s) output of 3, 6, and 11.

This macro probably helps allocating memory blocks that must have an aligned
block size, e.g. bitmaps.

regards,
Andreas
 
D

Doug Harrison [MVP]

kingdomzhf said:
Following macro is found inside Microsoft C++ source code:

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

The result between _INTSIZEOF(n) and sizeof(n) are always the same. I'm not sure why they defined this one. Specifically, what's the purpose of minus one for ?

Anybody has any ideas?

This is a way to round a non-negative integer up to the next multiple of
sizeof(int). For example, sizeof(short) is 2, but _INTSIZEOF(short) is 4.
Calculations like this are useful when writing things like heap managers, or
in general, when dealing with a granularity > 1, that is also a power of
two. If you let N be the exponent, then the calculation is:

(x + 2**N - 1) & ~(2**N - 1)

For x that isn't already a multiple of 2**N, it causes an overflow in the
low order N bits, and it completes the rounding process by masking off those
low order bits, producing a result which is the next closest multiple of
2**N greater than x. It's basically a ceiling function on multiples of
powers of two.
 
M

MerkX Zyban

No. It's a snowcone maker. No. A popcorn machine?


kingdomzhf said:
Following macro is found inside Microsoft C++ source code:

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

The result between _INTSIZEOF(n) and sizeof(n) are always the same. I'm
not sure why they defined this one. Specifically, what's the purpose of
minus one for ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top