Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Strange result

Strange result

Scheduled Pinned Locked Moved C / C++ / MFC
question
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    justin223
    wrote on last edited by
    #1

    Hello dwCopyLen = dwBufSize - dwSkipCount - HVD_HEADER_SIZE; how can dwCopyLen become 821 when: dwBufSize = 815 dwSkipCount = 0 #define HVD_HEADER_SIZE 1+2+4+1 //stx+USHORT+DWORD+char strange huh? If i split it to three lines: dwCopyLen = dwBufSize; dwCopyLen -= HVD_HEADER_SIZE; dwCopyLen -= dwSkipCount; I get the correct result. Why?

    M 1 Reply Last reply
    0
    • J justin223

      Hello dwCopyLen = dwBufSize - dwSkipCount - HVD_HEADER_SIZE; how can dwCopyLen become 821 when: dwBufSize = 815 dwSkipCount = 0 #define HVD_HEADER_SIZE 1+2+4+1 //stx+USHORT+DWORD+char strange huh? If i split it to three lines: dwCopyLen = dwBufSize; dwCopyLen -= HVD_HEADER_SIZE; dwCopyLen -= dwSkipCount; I get the correct result. Why?

      M Offline
      M Offline
      Martyn Pearson
      wrote on last edited by
      #2

      This comes down to the order in which the operands are evaluated. Your first line expands to 815 - 0 - 1 + 2 + 4 + 1 i.e. 814 + 2 + 4 + 1 = 821 Do #define HVD_HEADER_SIZE (1 + 2 + 4 + 1) to get the results you are expecting!

      J 1 Reply Last reply
      0
      • M Martyn Pearson

        This comes down to the order in which the operands are evaluated. Your first line expands to 815 - 0 - 1 + 2 + 4 + 1 i.e. 814 + 2 + 4 + 1 = 821 Do #define HVD_HEADER_SIZE (1 + 2 + 4 + 1) to get the results you are expecting!

        J Offline
        J Offline
        justin223
        wrote on last edited by
        #3

        Martyn Pearson wrote: This comes down to the order in which the operands are evaluated. Your first line expands to 815 - 0 - 1 + 2 + 4 + 1 i.e. 814 + 2 + 4 + 1 = 821 Do #define HVD_HEADER_SIZE (1 + 2 + 4 + 1) to get the results you are expecting! ofcourse :omg: Why didnt I think of that? :-D Many thanks!

        P 1 Reply Last reply
        0
        • J justin223

          Martyn Pearson wrote: This comes down to the order in which the operands are evaluated. Your first line expands to 815 - 0 - 1 + 2 + 4 + 1 i.e. 814 + 2 + 4 + 1 = 821 Do #define HVD_HEADER_SIZE (1 + 2 + 4 + 1) to get the results you are expecting! ofcourse :omg: Why didnt I think of that? :-D Many thanks!

          P Offline
          P Offline
          peterchen
          wrote on last edited by
          #4

          Lesson 1: Don't use macros unless you can't do without. const int HVD_HEADER_SIZE = 1 + sizeof(USHORT) + sizeof(DWORD) + sizeof(char); would have done the job. Lesson 2: If you have to embrace macros, embrace them correctly: braces go 'round the entire expression, and each argument :)


          "Der Geist des Kriegers ist erwacht / Ich hab die Macht" StS
          sighist | Agile Programming | doxygen

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups