Why is 4 bytes of RAM allocated for the parameter variable of a function if the type is short (2 bytes)?
At the request of dgzargo
Why is 4 bytes of RAM allocated for the parameter variable of a function if the type is short (2 bytes)?
At the request of dgzargo
First, in such a code, most likely, no memory will be allocated for the parameters at all - the parameters will be placed in the processor registers.
Secondly, even if your implementation decides to place parameters in memory, most likely it will align them either to the boundary of the native word of your platform, or (more likely) to the border of type int .
A special feature of the variant with alignment on the int type boundary is compatibility with the "old" conventions adopted for calling undeclared functions in the C language: arguments of the type short generate parameters of type int . Most likely this is one of the dominant factors in this case. Compatibility with agreements on the transfer of C language parameters is practically worthless, and there is no point in dealing with some unnecessary “parameter packing” for C ++.
For the same reason, even char arguments will be passed as full-length int , and float arguments as double .
PS You should not be interested in how much memory has been allocated for the transfer of your parameters. It does not matter. Inside the function, your parameters will behave like short values, and their initial values ​​will be passed correctly.
va_arg , and not through "pointer offset". - AnTIn your case, the parameters are passed through the stack. And since the program is thirty-two bit, then the stack quantum is corresponding. About the transfer of parameters in Visual Studio is written here and here .
On x86, when they are passed.
What is a stack frame and what and how it is located there is written here and here .
Source: https://ru.stackoverflow.com/questions/902342/
All Articles
int, i.e. by 4 bytes. - AnT