Results 1 to 2 of 2

Thread: Strict-aliasing and old-style-cast problem with C to C++ code

  1. #1
    Join Date
    Jun 2011
    Posts
    23
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Strict-aliasing and old-style-cast problem with C to C++ code

    Hi,

    I have some c code that I have put into my qt5 program but I get a lot of old-style-cast warnings which I would like to get rid of but don't quite know how to. I have tried reinterpret_cast but either I'm doing it incorrectly or it is not the right thing to do as it still results in the same error...

    Thanks in advance!

    Qt Code:
    1. warning: use of old-style cast [-Wold-style-cast]
    2. warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
    To copy to clipboard, switch view to plain text mode 

    Example code:
    Qt Code:
    1. unsigned int j,k;
    2. unsigned int* jj;
    3. char* cptr;
    4. char buff[50];
    5.  
    6. jj = (unsigned int*)&tmpY; //recast as int as expected in htonl
    7. j = htonl(*jj); //convert to network format
    8. cptr = (char*)&j; //recast as a char * so can use indexing
    9. sprintf(buff,"Y=%7.2f ",tmpY);
    10. tmp=buff;
    11. s.append(buff);
    12. for(i=0;i<YLen;i++)
    13. {
    14. msg[YIndex+i] = cptr[i];
    15. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Strict-aliasing and old-style-cast problem with C to C++ code

    reinterpret_cast, static_cast, dynamic_cast and const_cast are the c++ cast alternatives. Google around to read why to use one over the others, etc.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

Similar Threads

  1. Qt Creator Code Style settings are not being followed
    By mqduck in forum Qt Tools
    Replies: 0
    Last Post: 5th July 2016, 21:43
  2. Replies: 2
    Last Post: 23rd November 2012, 16:56
  3. Qt Style Sheet Vs Code
    By Ashutosh2k1 in forum Qt Programming
    Replies: 4
    Last Post: 4th May 2011, 07:45
  4. How to setMask without aliasing
    By burkav84 in forum Qt Programming
    Replies: 1
    Last Post: 10th April 2007, 17:51
  5. Cast problem
    By yellowmat in forum Newbie
    Replies: 3
    Last Post: 7th February 2006, 16:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.