How can I determine the word alighment role?

This is a discussion on How can I determine the word alighment role? within the C++ Programming forums, part of the General Programming Boards category; Some computer systems have alignment rules that must be taken into consideration when constructing complex types. The complex types may ...

  1. #1
    corn
    Guest

    How can I determine the word alighment role?

    Some computer systems have alignment rules that must be taken into consideration when constructing complex types. The complex types may be organized such that all base types start on an offset which is a multiple of their won sizeFor example, the offset of a 16-bit short, within a complex type could be 0, 16, 32, 48 or any other multiple of 16.

    My question is, how can I determine whether there is word alignment role exsiting in my computer system?

    Thanks.

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    This is not a computer dependency, it is a language/compiler issue. Check your compilers documentation.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    5
    Originally posted by adrianxw
    This is not a computer dependency, it is a language/compiler issue. Check your compilers documentation.
    Really? But in some documents I find it is computer system dependent. Could you please recommend some documents or information about it? Thanks.

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Computers have "natural" boundaries. When a 32 bit machine loads a 32 bit register, it does it quickly, so many compilers align variables on 32 bit boundaries. However, some languages are specified to byte align data.

    A Fortran COMMON block for example may begin on a 32 bit boundary, but within the COMMON block, the individual data elements are adjacent, (hence with Fortran coding, it is good practice to list the elements in size order). It HAS to be that way because you can declare the data differently in different functions, (i.e. In function a() your common block has a simple 32 bit integer, in function b() the common block is declared as a 4 byte array). You cannot allow the compiler to optimise that, because you do not know if an external library has been optimised in the same way.

    It takes more time and fiddling about to get the data onto non natural boundaries, and is therefore avoided. I can select the packing of my structures using compiler directives or pragmas, from MSDN...

    >>>
    Depending on the/Zp compiler option or the pack pragma, intervening space can be introduced to align member data on word or doubleword boundaries.
    <<<

    ... I am unaware of any general documents, all these types of thing tend to be vendor specific.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    5
    Originally posted by Salem
    Yeah, and the compiler takes care of it for you, so you don't have to.
    But I need to directly access each attribute of the complex types and modify the values of them. So I must know the address of each attribute before I modify it. How can I determine whether the word alignment role existing before I compute the address?
    Last edited by corn; 06-19-2003 at 04:03 AM.

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>> modify the values of them.
    Code:
    Class.x = 5;
    As Salem says, the compiler will sort that out. Whilst modifying the values, you do not need to know if the member x is a byte, a word or a double word. If you want to know how it's done, then I would say again...

    >>> Check your compilers documentation.

    ... if you want to change it, look up the options and/or pragmas available to you, these will also be found in in your compilers documentation. I would advise against changing the alignment unless you have a really good reason, because the compilers optimisation/applications performance can take a real hit.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  7. #7
    Registered User
    Join Date
    Jun 2003
    Posts
    5
    adrianxw:

    Yes, it is a easy way to modify value using "Class.x = 5;". However, the problem is that I don't know the name of these attributes when I modify them.

    Suppose there are a class like this:

    Code:
    class AObject{
              int a;
              double b;
              char c;
    };
    
    AObject A1=new AObject();
    I design a dll to modify the values of these objects. Before that, the intial address of this object instance and the structure of that object is known. So I need compute the address of each attribute and then modify it. It's obvious that I cannot do it as:

    Code:
    A1.b=0.5;
    I have to know whether I need take the word alignment rule into account when I compute the address. But how?

  8. #8
    Registered User
    Join Date
    Jun 2003
    Posts
    5
    Thanks.

    All of your suggestions are helpful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  2. please help with binary tree, urgent.
    By slickestting in forum C Programming
    Replies: 2
    Last Post: 07-22-2007, 08:55 PM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 06:49 PM
  4. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 06:44 PM
  5. Using 'if' with char arrays or string objects
    By c++_n00b in forum C++ Programming
    Replies: 36
    Last Post: 06-06-2002, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21