|
-
Jan 2nd, 2002, 07:32 PM
#1
Thread Starter
New Member
-
Jan 3rd, 2002, 02:17 PM
#2
Monday Morning Lunatic
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++.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jan 4th, 2002, 10:39 AM
#3
Thread Starter
New Member
-
Jan 4th, 2002, 12:06 PM
#4
Monday Morning Lunatic
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.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jan 5th, 2002, 05:54 PM
#5
Thread Starter
New Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|