Results 1 to 5 of 5

Thread: MS Compiler Config Ques

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2001
    Location
    Oxford, England
    Posts
    13

    Question MS Compiler Config Ques

    Hi

    I'm hoping this question is an easy one to answer .

    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

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2001
    Location
    Oxford, England
    Posts
    13

    Smile

    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

    Thanks again

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2001
    Location
    Oxford, England
    Posts
    13

    Smile

    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
  •  



Click Here to Expand Forum to Full Width