Thread: Important info!

  1. #1
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158

    Important info!

    I was just encountering a bug where assignment of a DWORD var wasn't working right.
    I was converting a float directly to a DWORD during the assignment. It worked most of the time, but every now and then it didn't.
    I'm not completely sure what causes this problem, but I'm guessing it has to do with the byte alignment of the var in the memory.
    So, just wanted to say... always convert your floats into longs before converting them to DWORDs, and convert your doubles to long-longs before converting them to QWORDs.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Do you have a short example which shows this?
    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.

  3. #3
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Yeah, but it's spread through several different files (not short) in a large (well, large for me) project.
    I found some links that seem to be very similar:
    http://bytes.com/forum/thread282607.html
    http://geekswithblogs.net/gyoung/arc.../30/80119.aspx

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    53
    Quote Originally Posted by Yarin View Post
    Yeah, but it's spread through several different files (not short) in a large (well, large for me) project.
    I found some links that seem to be very similar:
    http://bytes.com/forum/thread282607.html
    http://geekswithblogs.net/gyoung/arc.../30/80119.aspx
    I don't think it can help to cast your float to a long and then cast it to DWORD, because in a couple of Windows headers, you'll simply find

    Code:
    typedef unsigned long DWORD
    The first link you give multiplies the floating point value by UINT_MAX; I am not very surprised that things come out wrong in that case. The second link is just one of the many reasons to eschew .NET - it has nothing to do with C++ per se.

    Can't you make a (small) test program which shows your problem in a few lines of code?

    --
    Computer Programming: An Introduction for the Scientifically Inclined

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help displaying info from structure
    By kisiellll in forum C Programming
    Replies: 6
    Last Post: 04-04-2009, 12:51 PM
  2. Question about getting an info class from another Form
    By Joelito in forum C# Programming
    Replies: 0
    Last Post: 10-16-2006, 01:02 PM
  3. Help doing an e-mail program in c...
    By Tyler_Durden in forum C Programming
    Replies: 88
    Last Post: 01-02-2005, 03:12 PM
  4. Binary trees search problem...
    By Umoniel in forum C Programming
    Replies: 2
    Last Post: 02-22-2004, 02:29 PM
  5. Replies: 3
    Last Post: 12-06-2001, 05:30 AM