Thread: rarg, memcpy is annoying

  1. #1
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905

    rarg, memcpy is annoying

    Rarg!!!

    ok, i'm using memcpy to try and pull out some data from an array of unsigned chars.

    the way i'm doing this is: i read data from a file and put it in to a dynamically allocated array of unsigned chars.

    Then, when i need something out of it, i do a memcpy to get the data.

    Now, the way i'm trying to do this is, the files themselves have a bunch of unsigned ints and floats in them, so i need to do a memcpy like so:

    memcpy(floatVar, data, sizeof(float));

    or something like that. But when i try and do that, it won't compile. So, then i add a (void*) to the beginning of floatVar, and it compiles fine, but when i run the program, it crashes........rarg!!!

    anyone know how to copy memory over to a float variable?

    or should i just do it the "old-fashioned" way and just do everything AS i'm reading in the file? (the reason I'm doing it this way is so that if the program crashes mid-read, it won't lose the file data....)

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Is floatVar a pointer type? How about data? memcpy() wants addresses there.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    ...rarg?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    data is an unsigned char*

    floatvar is a float

  5. #5
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    i tried that, but when i run the program, it crashes

  6. #6
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    Originally posted by jverkoey
    i tried that, but when i run the program, it crashes
    How did you "dynamically" allocate data ?

    Did you create a normal array like a) byte data[iSomeSize] or is data a pointer to something like b)data= new byte[iSomeSize] ?

    In both cases, &data[0] should work thanks to the smart [] operator. This also allows you to specify a position in data from where you want to read the float (assuming there is more than just 4 bytes in "data").

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Disagreement about memcpy
    By ch4 in forum C Programming
    Replies: 9
    Last Post: 05-28-2009, 10:12 AM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Memcpy(); Errors...
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2006, 11:35 AM
  4. memcpy with 128 bit registers
    By grady in forum Linux Programming
    Replies: 2
    Last Post: 01-19-2004, 06:25 AM
  5. memcpy
    By doubleanti in forum C++ Programming
    Replies: 10
    Last Post: 02-28-2002, 04:44 PM