Like Tree1Likes
  • 1 Post By grumpy

Please help in solving the warning "cast increases required alignment of target type"

This is a discussion on Please help in solving the warning "cast increases required alignment of target type" within the C Programming forums, part of the General Programming Boards category; I have two structures as below Code: struct prefix { u_char a; u_char b; u_char c; u_char d; u_char e[4]; ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    53

    Please help in solving the warning "cast increases required alignment of target type"

    I have two structures as below

    Code:
    struct prefix
    {
       u_char a;
       u_char b;
       u_char c;
       u_char d;
       u_char e[4];
    };
    
    struct in_addr
    {
       union{
         u_int8_t addr8[16];
         u_int16_t addr16[8];
         u_int32_t addr32[4];
       } in6;
    }
    When I used the above structures as a function argument as below
    .....
    .....
    log(.......,(struct in_addr*)p->addr32,......); // where p is a pointer to structure prefix
    .....
    .....

    at that point, I am getting the warning as "cast increases required alignment of target type".

    Please help me

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    53
    Googled for solution but very confused among "_attribute_ ((packed));" and "_attribute_ ((alligned) (x)) ;", which one to use and why?

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,284
    I suspect you're asking the wrong question.

    A "struct prefix" is significantly smaller than a "struct in_addr", any attempt to use a struct prefix as a struct in_addr will almost certainly tromp areas of process memory that you don't intend.

    On the face of it, it appears you introduced that typecast to stop the compiler complaining about something else (eg you are trying to supply the address of a "struct prefix" to a function that expects a pointer to "struct addr32", and you've tried to use a typecast to force the compiler to permit that). Even if you do get such code to compile, all that will probably happen is that your program will crash when it is executed.

    Rather than trying to hack code, and then beat the compiler into submission with typecasts and pragmas or other non-standard settings to stop it complaining, it would be beneficial if you tried writing clean code.
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    53
    My question is correct grumpy. Because same kind of questions are there when googled, and they said about using "_attribute_ ((packed)); to the structure. But the problem here is the structure in_addr belongs to kernel. so, dont want to change that. and moreover the function "log" accepts variable arguments. And the argument I have shown you is one of the variable arguments I am passing.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    Where is your prototype for your log function?

    Alignment cast warnings work like this
    Code:
    void foo ( double *p ) {
      *p = 0;
    }
    int main ( ) {
      unsigned char v[sizeof(double)];
      foo( (double*)v );
    }
    Now whilst v has enough bytes to store a double, it may be aligned on a byte boundary, and not a double boundary.
    As such, when you try to dereference the pointer in foo, it will be an invalid address and the program will just crash.

    The correct way to convert a struct prefix into a struct in_addr would be to do something like
    Code:
    struct in_addr temp = { 0 };
    memcpy( temp.in6.addr32, p, sizeof(*p) );
    log(..., temp.in6.addr32 ... );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    53
    Guys, Thanks a lot for your replies.

    could you please look in to this link which is most similart to my problem and requesting you to respond

    Warning "cast increases required alignment of target type" - EmbDev.net

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,284
    Quote Originally Posted by karthik537 View Post
    My question is correct grumpy. Because same kind of questions are there when googled, and they said about using "_attribute_ ((packed)); to the structure.
    The fact of others having asked similar questions doesn't mean your question is the right one.

    Lemmings follow, regardless of the sense of following.

    Quote Originally Posted by karthik537 View Post
    But the problem here is the structure in_addr belongs to kernel. so, dont want to change that. and moreover the function "log" accepts variable arguments. And the argument I have shown you is one of the variable arguments I am passing.
    Yes, but if you use a typecast to convince the compiler to permit passing a different data type to a function than the function expects, it is your responsibility to ensure that what you pass is acceptable to the called function. One aspect of compatibility is that the data structure you pass cannot be smaller than what the function expects. And that is exactly what you are doing.

    The fact you can bludgeon the compiler into accepting that does not make the data you pass suddenly valid. However, such tricks do not make invalid data become valid - they simply stop the compiler from complaining about the fact you are passing invalid data. The question you need to be asking is how to ensure the data you pass is valid, not about how to force the compiler to stop complaining about passing invalid data.
    rags_to_riches likes this.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-12-2011, 05:59 PM
  2. A "wrong type" warning
    By zel in forum C Programming
    Replies: 25
    Last Post: 09-08-2010, 09:29 AM
  3. Replies: 6
    Last Post: 04-05-2010, 09:09 AM
  4. Replies: 1
    Last Post: 12-05-2008, 01:51 PM
  5. Replies: 2
    Last Post: 05-21-2008, 02:52 PM

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