__unaligned before and after the * (64bit)

Discussion in 'C Programming' started by Udi, Mar 19, 2008.

  1. Udi

    Udi Guest

    Hi All,
    I'm not sure I understand the difference between placing the
    __unaligned before or after the *:
    I was trying to handle the C4366 warning - "The result of the unary
    '&' operator may be unaligned") and used the '__unaligned' modifier as
    suggested,
    but ended up with C4090 - "different '__unaligned' qualifiers". (See
    below)
    However, moving the __unaligned keyword after the ' * '
    solved the warning but I'm not sure I solved the probelm.


    with no __unaligned keyword --> Warning C4366
    ```````````````````````````````````````````````````````````````````````
    void List_Clear(List *pList) ;
    :
    List * pSubscribersList = NULL;

    pSubscribersList = (List *)&(p->subscribersList); //C4366: The result
    of the unary '&' operator may be unaligned
    List_Clear(pSubscribersList);




    __unaligned before the * --> warning C4090
    ```````````````````````````````````````````````````````````````
    void List_Clear(List *pList) ;
    :
    List __unaligned * pSubscribersList = NULL;

    pSubscribersList = (List __unaligned *)&(p->subscribersList);
    List_Clear(pSubscribersList); //warning C4090: 'function' : different
    '__unaligned' qualifiers


    __unaligned after the * --> no warnings
    ````````````````````````````````````````````````````````
    void List_Clear(List *pList) ;
    :
    List * __unaligned pSubscribersList = NULL;

    pSubscribersList = (List * __unaligned)&(p->subscribersList);
    List_Clear(pSubscribersList); // OK - no warning



    Can anyone explain what's the difference between the last two
    examples?
    I'm using VS2005 compiling to 64 bit.
    Thanks!
     
    Udi, Mar 19, 2008
    #1
    1. Advertising

  2. On 19 Mar, 08:53, Udi <> wrote:

    > I'm not sure I understand the difference between placing the
    > __unaligned before or after the *:


    <snip>

    > I'm using VS2005 compiling to 64 bit.


    __unaligned isn't part of standard C. You needd to ask ona compler
    specific news group. Try I Microsoft related ng.


    --
    Nick Keighley
     
    Nick Keighley, Mar 19, 2008
    #2
    1. Advertising

  3. Nick Keighley <> writes:
    > On 19 Mar, 08:53, Udi <> wrote:
    >
    >> I'm not sure I understand the difference between placing the
    >> __unaligned before or after the *:

    >
    > <snip>
    >
    >> I'm using VS2005 compiling to 64 bit.

    >
    > __unaligned isn't part of standard C. You needd to ask ona compler
    > specific news group. Try I Microsoft related ng.


    Better yet, re-write the code to avoid the need to use __unaligned.
    The original poster appears to be writing code for a linked list.
    There's no need to use any implementation-specific extensions for such
    a relatively straightforward task.

    Apparently the compiler's warning "The result of the unary '&'
    operator may be unaligned" included a suggestion to use __unaligned.
    I strongly suspect that suggestion was a poor one.

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Mar 19, 2008
    #3
  4. Udi wrote:
    > I'm not sure I understand the difference between placing the
    > __unaligned before or after the *:


    I guess, it works like 'const', which applies to the left unless it is at
    the leftmost side, then it applies to the right. However, it is, as others
    pointed out, non-standard, so you have to consult the compiler docs.

    > I was trying to handle the C4366 warning - "The result of the unary
    > '&' operator may be unaligned") and used the '__unaligned' modifier as
    > suggested, but ended up with C4090 - "different '__unaligned' qualifiers".
    > (See below)
    > However, moving the __unaligned keyword after the ' * '
    > solved the warning but I'm not sure I solved the probelm.


    I don't think so...

    >


    > void List_Clear(List *pList) ;
    > :
    > List * pSubscribersList = NULL;
    >
    > pSubscribersList = (List *)&(p->subscribersList); //C4366: The result
    > of the unary '&' operator may be unaligned


    Well, the first problem here is that you are using casts, which is typically
    a sign that something's wrong. Remove those, and you won't need any
    unaligned attributes. If it doesn't compile then, your types simply don't
    match, but adding them doesn't change that. If you can't do it yourself or
    want to verify the solution is correct, please boil your problem down to a
    minimal but complete example, in particular guessing
    what 'p->subscribersList' could be is pretty hart.

    Uli
     
    Ulrich Eckhardt, Mar 19, 2008
    #4
  5. Udi

    Udi Guest

    On Mar 19, 11:18 pm, Ulrich Eckhardt <> wrote:
    > Udiwrote:
    > > I'm not sure I understand the difference between placing the
    > > __unaligned before or after the *:

    >
    > I guess, it works like 'const', which applies to the left unless it is at
    > the leftmost side, then it applies to the right. However, it is, as others
    > pointed out, non-standard, so you have to consult the compiler docs.
    >
    > > I was trying to handle the C4366 warning - "The result of the unary
    > > '&' operator may be unaligned") and used the '__unaligned' modifier as
    > > suggested, but ended up with C4090 - "different '__unaligned' qualifiers".
    > > (See below)
    > > However, moving the __unaligned keyword after the ' * '
    > > solved the warning but I'm not sure I solved the probelm.

    >
    > I don't think so...
    >
    >
    >
    > > void List_Clear(List *pList) ;
    > > :
    > > List  * pSubscribersList = NULL;

    >
    > > pSubscribersList = (List *)&(p->subscribersList); //C4366: The result
    > > of the unary '&' operator may be unaligned

    >
    > Well, the first problem here is that you are using casts, which is typically
    > a sign that something's wrong. Remove those, and you won't need any
    > unaligned attributes. If it doesn't compile then, your types simply don't
    > match, but adding them doesn't change that. If you can't do it yourself or
    > want to verify the solution is correct, please boil your problem down to a
    > minimal but complete example, in particular guessing
    > what 'p->subscribersList' could be is pretty hart.
    >
    > Uli


    Thanks All,
    I agree I shouldn't use the __unaligned, that's why I'm writing to
    figure out how.

    Here are the full types and example:

    typedef struct
    {
    HANDLE hAccess; // Mutex
    HANDLE hCanRead; // Event
    HANDLE hCanWrite; // Event
    HANDLE hWriterMutex; // Mutex
    DWORD dwTlsSlot;

    DWORD dwTotalReaderThreads;
    DWORD dwTotalWriterThreads;
    } tReadWriteLock;

    typedef struct _ListItem
    {
    void *pData;
    struct _ListItem *pNextItem;
    } ListItem;

    typedef struct
    {
    ListItem *pHead;
    DWORD dwLength;
    tReadWriteLock *pLock;
    } List;

    typedef struct
    {
    DWORD id;
    List subscribersList;
    } IndicatorSubscribers;


    with no __unaligned keyword --> Warning C4366
    ```````````````````````````````````````````````````````````````````````
    void List_Clear(List *pList) ;
    :
    List * pSubscribersList = NULL;


    pSubscribersList = (List *)&(p->subscribersList); //C4366: The result
    of the unary '&' operator may be unaligned
    List_Clear(pSubscribersList);


    __unaligned before the * --> warning C4090
    ```````````````````````````````````````````````````````````````
    void List_Clear(List *pList) ;
    :
    List __unaligned * pSubscribersList = NULL;


    pSubscribersList = (List __unaligned *)&(p->subscribersList);
    List_Clear(pSubscribersList); //warning C4090: 'function' : different
    '__unaligned' qualifiers


    __unaligned after the * --> no warnings
    ````````````````````````````````````````````````````````
    void List_Clear(List *pList) ;
    :
    List * __unaligned pSubscribersList = NULL;


    pSubscribersList = (List * __unaligned)&(p->subscribersList);
    List_Clear(pSubscribersList); // OK - no warning




    As you can see these are all pretty simple typedefs.
    So the 'p' in 'p->subscribersList' is of type 'IndicatorSubscribers'.

    Any ideas now?
    (Still I didn't get the difference between placing the __unaligned
    before and after the *.)

    Thanks,
    Udi
     
    Udi, Mar 23, 2008
    #5
  6. Udi

    Flash Gordon Guest

    Udi wrote, On 23/03/08 07:11:

    <snip>

    > typedef struct
    > {
    > ListItem *pHead;
    > DWORD dwLength;
    > tReadWriteLock *pLock;
    > } List;
    >
    > typedef struct
    > {
    > DWORD id;
    > List subscribersList;
    > } IndicatorSubscribers;
    >
    >
    > with no __unaligned keyword --> Warning C4366
    > ```````````````````````````````````````````````````````````````````````
    > void List_Clear(List *pList) ;
    > :
    > List * pSubscribersList = NULL;
    >
    >
    > pSubscribersList = (List *)&(p->subscribersList); //C4366: The result
    > of the unary '&' operator may be unaligned
    > List_Clear(pSubscribersList);


    <snip>

    > Any ideas now?
    > (Still I didn't get the difference between placing the __unaligned
    > before and after the *.)


    Start off by getting rid of ALL of the casts. Doing the sort of thing it
    looks like you are trying to do does not require casts if you do it
    correctly. Then produce a small *complete* compilable example that gives
    the warning, preferably without using "//" style comments as they do not
    survive line wrapping. Your example obviously is not complete as you
    have code that is not inside a function.
    --
    Flash Gordon
     
    Flash Gordon, Mar 23, 2008
    #6
  7. Udi <> writes:

    > On Mar 19, 11:18 pm, Ulrich Eckhardt <> wrote:

    <snip>
    >> Well, the first problem here is that you are using casts, which is typically
    >> a sign that something's wrong. Remove those, and you won't need any
    >> unaligned attributes.

    <snip>
    > I agree I shouldn't use the __unaligned, that's why I'm writing to
    > figure out how.


    But you left the cast.

    > typedef struct
    > {
    > DWORD id;
    > List subscribersList;
    > } IndicatorSubscribers;
    >
    >
    > with no __unaligned keyword --> Warning C4366
    > ```````````````````````````````````````````````````````````````````````
    > void List_Clear(List *pList) ;
    > :
    > List * pSubscribersList = NULL;
    >
    >
    > pSubscribersList = (List *)&(p->subscribersList); //C4366: The result
    > of the unary '&' operator may be unaligned

    <snip>
    > As you can see these are all pretty simple typedefs.
    > So the 'p' in 'p->subscribersList' is of type
    > 'IndicatorSubscribers'.


    You did not show the definition of p. If it is of type pointer to
    IndicatorSubscribers, and the line in question gives you an error when
    you remove the cast (it is not needed -- trust me!) then the problem
    is elsewhere.

    Most likely, something like a #pragma elsewhere or a compiler option
    is causing the compiler to pack the structure in a way that violates
    the assurances given by the language standard. If that is the case
    (and you can't change it) you will have to use something non-standard
    to solve the problem.

    --
    Ben.
     
    Ben Bacarisse, Mar 23, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexander Schmolck
    Replies:
    2
    Views:
    374
    David Rushby
    Jan 28, 2006
  2. robert

    Java 6 and 64bit max memory

    robert, Dec 29, 2006, in forum: Java
    Replies:
    3
    Views:
    2,448
    Daniel Pitts
    Dec 30, 2006
  3. Using 32bit and 64bit gcc

    , Aug 30, 2007, in forum: C Programming
    Replies:
    4
    Views:
    422
    Keith Thompson
    Aug 30, 2007
  4. cman
    Replies:
    19
    Views:
    2,108
    James Kanze
    Nov 20, 2007
  5. Zorro
    Replies:
    5
    Views:
    298
    Zorro
    Jul 26, 2008
Loading...

Share This Page