Important info!

This is a discussion on Important info! within the C++ Programming forums, part of the General Programming Boards category; I was just encountering a bug where assignment of a DWORD var wasn't working right. I was converting a float ...

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

    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.
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,609
    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
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  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, 02:12 PM
  4. Binary trees search problem...
    By Umoniel in forum C Programming
    Replies: 2
    Last Post: 02-22-2004, 01:29 PM
  5. Replies: 3
    Last Post: 12-06-2001, 04:30 AM

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