-
MS Compiler Config Ques
Hi
I'm hoping this question is an easy one to answer :D.
I have 3 variables, 1 is a short integer and the other 2 are long integer variable types. After compiling and running the program I write the address values of all three variable types to the screen which currently give me:
- 0x0066FDF4
- 0x0066FDF0
- 0x0066FDEC
Ok, that's all fine except the first address was the short integer and the second address in that list was the first long integer meaning it should have a 2 byte difference from the first value right?
If I use the sizeof command to get the values of different data types I get correct values for my OS (win98). Is there an option in the MSVC compiler to make the short integer types actually get compiled with a size of 2 bytes?
I may have just skipped over something really stupid btw ;) lol.
Thanx
-
The variable is only two bytes.
It's aligned on a 4-byte boundary because that is the most efficient way for an x86 processor to handle it (i.e. you don't actually have any benefit for a single char variable - you could just use int.
However this doesn't apply for an array - since that's a single variable.
If you want to disable this (be VERY careful since it can mess things up and seriously knacker your performance) then you can set the packing options to 1 byte in Project Settings->C++.
-
OK I'm a little confused. If the variable is only 2 bytes yet when reading the memory it says it takes 4 bytes up, what is the point in ever creating a variable of "short" type if it's going to take up 4 anyway?
If though, you say that the short IS taking up 2 bytes, is there anyway to actually read into the memory (like using the addressof operator) to prove this?
Oh I can't see the packing options either :(:rolleyes:
Thanks again
-
Well, when you create a short, it will only ever write into two bytes, whatever value you give.
The limitation is that variables have to start on a 4-byte boundary, it doesn't mean they can't be smaller than that. So, if you control the variable's environment, you can have them aligned to a 2-byte boundary.
Do a search in the MSVC documentation for #pragma pack - that's how to do it locally.
You can see what's in there through:
Code:
short x = 0;
short y = 0xFFFF;
int *z = (int*)&x;
cout << *z << endl;
If that doesn't give an access violation then it shouldn't print a combination of x and y.
-
Thanks for your time but I think this is getting pointlessly beyond me for the moment :(
I'll just take it that it IS taking up 2 bytes like you said and figure out technical details to how the variables boundarys work later.