How to fix misaligned assignment statements in the source code?

This is a discussion on How to fix misaligned assignment statements in the source code? within the C++ Programming forums, part of the General Programming Boards category; It's the same example....

  1. #16
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    It's the same example.
    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.

  2. #17
    Registered User
    Join Date
    May 2006
    Posts
    19
    Hello All,

    I keep fixing misaligned statements as Salem suggested.

    But now I have some statement I can't get working
    On line number 112 I got this while compiling -
    Code:
    RTCPUtilitiesLib/RTCPSRPacket.h: In member function `void RTCPSRPacket::SetSSRC(UInt32)':
    RTCPUtilitiesLib/RTCPSRPacket.h:112: warning: cast from `char (*)[132]' to `UInt32*' increases required alignment of target type
    This source is looking like this:
    Code:
        109 inline void RTCPSRPacket::SetSSRC(UInt32 inSSRC)
        110 {
        111     // Set SSRC in SR
        112     ((UInt32*)&fSenderReportBuffer)[1] = htonl(inSSRC);

    I've tried to fix this (112) line with:

    UInt32 temp1; memcpy(&temp1, htonl(inSSRC), sizeof temp1); (&fSenderReportBuffer)[1]=&temp1;
    and with -
    UInt32 temp1; memcpy(&temp1, &htonl(inSSRC), sizeof temp1); (&fSenderReportBuffer)[1]=temp1;

    But still I get these messages while compiling:
    Code:
    RTCPUtilitiesLib/RTCPSRPacket.h: In member function `void RTCPSRPacket::SetSSRC(UInt32)':
    RTCPUtilitiesLib/RTCPSRPacket.h:112: error: incompatible types in assignment of `UInt32*' to `char[132]'
    Any help/suggestions on how to fix this misaligned statement?
    Last edited by biggyK; 06-22-2006 at 02:38 AM.

  3. #18
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    Guessing that fSenderReportBuffer is
    char fSenderReportBuffer[132];

    I would do
    Code:
    unsigned long temp = htonl(inSSRC);
    memcpy ( &fSenderReportBuffer[sizeof(UInt32)], &temp, sizeof(temp) );
    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.

  4. #19
    Registered User
    Join Date
    May 2006
    Posts
    19
    Thank you very much Salem.

    Your solution is working!!!

  5. #20
    Registered User
    Join Date
    May 2006
    Posts
    19
    Salem ,

    Sorry for bothering with the same issue, but...

    As you can see on the lines 112,115,119...
    Code:
    112:
    ((UInt32*)&fSenderReportBuffer)[1] = htonl(inSSRC);
    
    115:
    ((UInt32*)&fSenderReportBuffer)[8] = htonl(inSSRC);
    
    119:
    ((UInt32*)&fSenderReportBuffer)[(fSenderReportSize >> 2) + 1] = htonl(inSSRC);
    We have different values inside
    fSenderReportBuffer[here is the value that changes on all the lines]

    Where should I insert the appropriate change so it would meet your solution for every line:
    Code:
    unsigned long temp = htonl(inSSRC);
    memcpy ( &fSenderReportBuffer[sizeof(UInt32)], &temp, sizeof(temp) )

  6. #21
    Registered User
    Join Date
    May 2006
    Posts
    19
    Hi,

    Should this code
    Code:
    unsigned long temp = htonl(inSSRC);
    memcpy ( &fSenderReportBuffer[sizeof(UInt32)], &temp, sizeof(temp) );
    look like this on line 119? :

    Code:
    unsigned long temp = htonl(inSSRC);
    memcpy ( &fSenderReportBuffer[(fSenderReportSize >> 2) + 1], &temp, sizeof(temp) );
    or like this:

    Code:
    unsigned long temp = htonl(inSSRC);
    memcpy ( &fSenderReportBuffer[sizeof(UInt32)], &temp, sizeof(temp) );
    (&fSenderReportBuffer)[(fSenderReportSize >> 2) + 1]=temp;
    Last edited by biggyK; 06-26-2006 at 07:53 AM.

  7. #22
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    > ((UInt32*)&fSenderReportBuffer)[1] = htonl(inSSRC);
    Recall this line became
    memcpy ( &fSenderReportBuffer[sizeof(UInt32)], &temp, sizeof(temp) );

    More generally, I could have written
    memcpy ( &fSenderReportBuffer[sizeof(UInt32)*1], &temp, sizeof(temp) );
    where the 1 is the subscript you would like had the array been a proper array of UInt32.
    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.

  8. #23
    Registered User
    Join Date
    May 2006
    Posts
    19
    Salem,

    Thanks for the explanations.

  9. #24
    Registered User
    Join Date
    May 2006
    Posts
    19
    Hi,

    Continuing to fix misaligned statements and need some help.

    1) I got this error:
    Server.tproj/RTCPTask.cpp:95: warning: cast from `UDPDemuxerTask*' to `RTPStream*' increases required alignment of target type
    In file included from Server.tproj/RTPSessionInterface.h:41,
    from Server.tproj/RTPSession.h:48,
    from Server.tproj/RTPSession.cpp:36:

    This is how the source line looks like:
    Code:
    95  RTPStream* theStream = (RTPStream*)theDemuxer->GetTask(theRemoteAddr, theRemotePort);
    I've tried to fix it with -
    Code:
    struct RTPStream temp; memcpy (&temp, theDemuxer->GetTask(theRemoteAddr, theRemotePort), sizeof temp); RTPStream* theStream =&temp;
    or this -
    Code:
    RTPStream temp(theDemuxer->GetTask(theRemoteAddr,  theRemotePort)); RTPStream* theStream =&temp;
    But I keep getting compilation errors like these:
    Code:
    Server.tproj/RTCPTask.cpp: In member function `virtual SInt64 RTCPTask::Run()':
    Server.tproj/RTCPTask.cpp:95: error: no matching function for call to `RTPStream::RTPStream(UDPDemuxerTask&)'
    Server.tproj/RTPStream.h:57: note: candidates are: RTPStream::RTPStream(const RTPStream&)
    Server.tproj/RTPStream.h:66: note:                 RTPStream::RTPStream(UInt32, RTPSessionInterface*)
    make: *** [Server.tproj/RTCPTask.o] Error 1
    That can I do here?

    2) Another problem is like this:
    I got htis error:
    Code:
    Server.tproj/RTPSessionInterface.cpp: In static member function `static void* RTPSessionInterface::TimeConnected(QTSSDictionary*, UInt32*)':
    Server.tproj/RTPSessionInterface.cpp:277: warning: cast from `QTSSDictionary*' to `RTPSessionInterface*' increases required alignment of target type
    Server.tproj/RTPSessionInterface.cpp: In static member function `static void* RTPSessionInterface::CurrentBitRate(QTSSDictionary*, UInt32*)':
    Server.tproj/RTPSessionInterface.cpp:287: warning: cast from `QTSSDictionary*' to `RTPSessionInterface*' increases required alignment of target type
    Server.tproj/RTPSessionInterface.cpp: In static member function `static void* RTPSessionInterface::PacketLossPercent(QTSSDictionary*, UInt32*)':
    Server.tproj/RTPSessionInterface.cpp:298: warning: cast from `QTSSDictionary*' to `RTPSessionInterface*' increases required alignment of target type
    This is how the source looks like -
    Code:
        277     RTPSessionInterface* theSession = (RTPSessionInterface*)inSession; 
    
        287     RTPSessionInterface* theSession = (RTPSessionInterface*)inSession;
    
        298     RTPSessionInterface* theSession = (RTPSessionInterface*)inSession;

    I've tried to fix it like this -
    Code:
    277   struct RTPSessionInterface temp; memcpy (&temp, inSession, sizeof temp); RTPSessionInterface* theSession =&temp;
    287   struct RTPSessionInterface temp1; memcpy (&temp1, inSession, sizeof temp1); RTPSessionInterface* theSession =&temp1;
    298   struct RTPSessionInterface temp2; memcpy (&temp2, inSession, sizeof temp2); RTPSessionInterface* theSession =&temp2;
    But keep getting errors for these fixes:
    Code:
    Server.tproj/RTPSessionInterface.cpp: In static member function `static void* RTPSessionInterface::TimeConnected(QTSSDictionary*, UInt32*)':
    Server.tproj/RTPSessionInterface.cpp:277: error: cannot declare variable `temp' to be of type `RTPSessionInterface'
    Server.tproj/RTPSessionInterface.cpp:277: error:   because the following virtual functions are abstract:
    CommonUtilitiesLib/Task.h:94: error:  virtual SInt64 Task::Run()
    Server.tproj/RTPSessionInterface.cpp: In static member function `static void* RTPSessionInterface::CurrentBitRate(QTSSDictionary*, UInt32*)':
    Server.tproj/RTPSessionInterface.cpp:287: error: cannot declare variable `temp1' to be of type `RTPSessionInterface'
    Server.tproj/RTPSessionInterface.cpp:287: error:   since type `RTPSessionInterface' has abstract virtual functions
    Server.tproj/RTPSessionInterface.cpp: In static member function `static void* RTPSessionInterface::PacketLossPercent(QTSSDictionary*, UInt32*)':
    Server.tproj/RTPSessionInterface.cpp:298: error: cannot declare variable `temp2' to be of type `RTPSessionInterface'
    Server.tproj/RTPSessionInterface.cpp:298: error:   since type `RTPSessionInterface' has abstract virtual functions
    make: *** [Server.tproj/RTPSessionInterface.o] Error 1
    How can I fix these errors here?


    Thanks in advance for your replies.

  10. #25
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Whoa! Far too many casts! You're hiding the real errors.


    For example, demuxer->GetTask obviously returns a UDPDemuxerTask*. Why do you try to asssign it to an RTPStream*? Is RTPStream a subclass of UDPDemuxerTask? Then use dynamic_cast. Otherwise you're just doing something totally illegal.

    Ecactly the same issue in your second problem. The source is QTSSDictionary*, the target RTPSessionInterface*. What makes you think you can just assign them?

    C++ is strongly typed for a reason.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #26
    Registered User
    Join Date
    May 2006
    Posts
    19
    This is not my code.

    This is part of DSS (Darwin Streaming Server of Apple).
    I'm just trying to fix some misaligned statements, which preventing to run DSS on my architecture.

    I'm not a C++ programmer, just have a basic knowledge and trying to get some help/advice here.

  12. #27
    Registered User
    Join Date
    May 2006
    Posts
    19
    Hello Salem,

    I'm really lost with code (I've posted above on 07-08-2006, 10:31 AM).

    Could you please review and tell/recommend me how to deal with it?

  13. #28
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    > Could you please review and tell/recommend me how to deal with it?
    You really need to get in contact with the authors / maintainers at this point.

    > warning: cast from `UDPDemuxerTask*' to `RTPStream*' increases required alignment of target type
    Fixing alignment of say UINT32 is one thing, fixing class pointers is another.

    Especially if both things are part of the same class heirarchy, and up/down casting is supposed to do "the right thing" by the rules of C++.
    Understanding how those classes are supposed to interact is far more work than I'm prepared to take on at the moment - maybe someone else would help you if you'd care to post an announcement in the projects and recruitment board (if the author isn't willing to help). This is sounding like a significant task all of a sudden.


    If this isn't happening, then something serious has gone wrong, and the approach so far just isn't going to work anymore IMO.

    Are you still getting bus errors when you try and run what you've produced so far ?
    That is, have you done enough so that it now runs when you turn off the alignment warning?
    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.

  14. #29
    Registered User
    Join Date
    May 2006
    Posts
    19
    Hello Salem,

    Thanks for your reply.
    I still get "Bus Errors".

    Seems to me that I should install Solaris 10 on that machine (DSS reported to compile and run on that OS).

    Thanks for all your help

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-27-2006, 02:21 PM
  2. DxEngine source code
    By Sang-drax in forum Game Programming
    Replies: 5
    Last Post: 06-26-2003, 06:50 PM
  3. Lines from Unix's source code have been copied into the heart of Linux????
    By zahid in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 05-19-2003, 04:50 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. C source code for int25 or code help
    By Unregistered in forum C Programming
    Replies: 0
    Last Post: 09-26-2001, 03:04 AM

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