473,385 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

_INTSIZEOF(n)

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
Nov 16 '05 #1
4 3011
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" <an*******@discussions.microsoft.com> wrote in message
news:48**********************************@microsof t.com...
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

Nov 16 '05 #2
Hi,

"kingdomzhf" <an*******@discussions.microsoft.com> wrote in message
news:48**********************************@microsof t.com...
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
Nov 16 '05 #3
kingdomzhf wrote:
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.

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 16 '05 #4
No. It's a snowcone maker. No. A popcorn machine?
"kingdomzhf" <an*******@discussions.microsoft.com> wrote in message
news:48**********************************@microsof t.com...
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

Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Suzanne Vogel | last post by:
'stdarg.h' defines the 'va_arg' type to use in passing variable numbers of parameters to functions. The example of its use given in the Dinkumware documentation *seems* to imply that the 'va_arg'...
4
by: Sekhar | last post by:
While using variable arguments we have to initialize variable argument like va_start( arg_ptr, prevParam ); Can any body explain what is the significance of second parameter(prevParam) while...
29
by: candy_init | last post by:
Hi all, I just came across the following program: #include <stdio.h> int main() { float a = 12.5; printf("%d\n", a); printf("%d\n", *(int *)&a); return 0;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.