Thread: Critique my .wav writer code

  1. #16
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by algorism
    I believe you are mistaken. *(uint32_t*)"RIFF" is not a hack. It does exactly what is wanted
    I called it a hack because it was opaque to me. Why is 0x46464952 generated one way okay and the other way has endianess issues? It's not clear just from an "I said so" answer.

  2. #17
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    Quote Originally Posted by jimblumberg View Post
    True but there's nothing stopping you from using const qualified variables. Casts, should only be used as a last resort.

    Jim
    Absolutely. C++ uses const for constants not #define preprocessor macros, however even using const variables you are still likely to want the cast. As Algorism points out the cast is essentially correct. However I don't know what he means by big and little endian problems as WAV is a M$ file format and all M$ operating systems are little endian.
    Last edited by Hobbit; 12-03-2016 at 10:53 AM.

  3. #18
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by whiteflags View Post
    I called it a hack because it was opaque to me. Why is 0x46464952 generated one way okay and the other way has endianess issues? It's not clear just from an "I said so" answer.
    I never said or implied "I said so". I honestly thought you understood endianness so therefore didn't consider explaining the technique in detail.

    The whole point is that we are not trying to generate 0x46464952 at all. Yes, that's what we want on a little-endian system, which in a sense "reverses" the bytes when it stores them in memory, storing 0x52 ('F') lowest in memory, then 0x49, 0x46, 0x46. But on a big-endian system, the bytes are stored in the opposite order so the hex value must be 0x52494646 to get "RIFF" into memory the way we want.

    With the "hack" we put the bytes into memory in the exact order we want them and then interpret them as a 32-bit unsigned integer on the current platform.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Critique my code
    By Aslaville in forum C Programming
    Replies: 2
    Last Post: 05-09-2015, 03:38 AM
  2. Please critique my source code - Craps Game
    By bos1234 in forum C Programming
    Replies: 8
    Last Post: 09-20-2013, 01:22 PM
  3. C code critique
    By mnd22 in forum C Programming
    Replies: 8
    Last Post: 12-23-2011, 10:05 AM
  4. Critique my code?
    By bigbadbowlindud in forum C++ Programming
    Replies: 20
    Last Post: 06-24-2004, 12:54 PM
  5. could ya'll critique this code
    By linuxdude in forum C Programming
    Replies: 0
    Last Post: 02-19-2004, 09:55 PM

Tags for this Thread