Thread: passing around a float

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

    passing around a float

    ok, here's my code:

    Code:
    #include <stream.h>   // Header file i made that just has the basic includes for doing random dos stuff
    
    unsigned char* memory;
    
    void main()
    {
    	memory=new unsigned char[6];
    	float test=-7;
    	memcpy(&memory,&test,sizeof(float));
    
    	float test2=0;
    	memcpy(&test2,memory,sizeof(float));
    	printf("%f",test2);
    }

    rarg, this isn't the problem, scroll down to the post below to see what the actual problem is:::
    Last edited by jverkoey; 08-21-2003 at 05:00 PM.

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    1,619

    Re: passing around a float

    Multiple errors.

    1) Main must return an int. Not void. Ever.
    2) You pass &memory to the first memcpy -- that isn't what you want; memory is already a pointer. You're trying to pass a pointer to the buffer, not a pointer to a pointer to a buffer. The first memcpy will trash your stack.
    3) You allocate memory you do not free.

    Additionally,
    4) You use a global variable without cause.
    5) Your memory buffer might be too small -- you are not allowed to make assumptions about sizeof(float), if it's greater than 6, you buffer overflow.
    6) You deliberately circumvent the C++ type checking system without a reason.

    Code:
    #include <stream.h>   // Header file i made that just has the basic includes for doing random dos stuff
    
    unsigned char* memory;
    
    int main()
    {
    	memory=new unsigned char[6];
    	float test=-7;
    	memcpy(memory,&test,sizeof(float));
    
    	float test2=0;
    	memcpy(&test2,memory,sizeof(float));
    	printf("%f",test2);
    	delete[] memory;
    }
    But the better way to do this is:

    Code:
    #include <stream.h>   // Header file i made that just has the basic includes for doing random dos stuff
    
    int main()
    {
    	float * memory=new float;
    	float test=-7;
    	*memory = test;
    
    	float test2=0;
    	test2 = *memory;
    	printf("%f",test2);
    	delete memory;
    }

  3. #3
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    arg, i was just writing that program as a quicky, i didn't think you'd want to see my whole program (which has a LOT more code than the above)

    I tried to simplify it as best i could and I'm sorry if i didn't do my little debug program perfectly lol.

    -edit-
    and also, i did the unsigned char* thing for a reason The way i made the program is so that it reads in a file in to a big buffer (which is an unsigned char*). I dynamically allocate memory for it, and when i'm done with the file, i free it from memory.

    So, the point of the above program is to show off a simplified version of loading a file, say it was 6 bytes big. And the first 4 bytes were a float variable. So, I "write" the file, and then i "read" it right after that in the program above, this is just showing off a simple concept that I'm trying to get to work. However, in my other program, for some reason it can't memcpy the floats correctly.

    In my other program, i have a section of data that is equivalent to this:
    bf 00 00 00
    bf 00 00 00
    bf 00 00 00
    and those are SUPPOSED to be 3 floating point variables, equaling the same as:
    -0.5
    -0.5
    -0.5
    however, when i read the file in, i do a memcpy for each of the 3 floats and place them in to an X,Y,Z structure (where X,Y, and Z are each floats). Then, when i try and output the floats to see what they're equal to, it always outputs 0's.

    Then, as a test, I used an unsigned int variable and got the first 4 bytes from the data stream. I then outputted the unsigned int as a %x so i could see the hex form, and it was equal to BF000000....just like it was supposed to. So, I KNOW for a fact that the data stream DOES have the correct data in it, but for some reason, when i do memcpy, it doesn't copy the data correctly in to the float variable........

    so, if you can see my dillemma, i would greatly appreciate help.
    Last edited by jverkoey; 08-21-2003 at 04:59 PM.

  4. #4
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    OK, i think i may have found the problem, but to fix it, i need to know how to flip floating point variables around. I need to be able to flip it like so:

    Before:
    1 2 3 4

    After:
    4 3 2 1

    Because it appears that memcpy flips the floating point var around (ROAR!!! I HATE ENDIANS!!). I need to be able to flip it back around somehow.........does anyone know how? You can't use << >> on floats, so I'm wondering if there is a function or something that'll do that?

  5. #5
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    ok, i just made a function that would take the data in the unsigned char* buffer and swap it there, BEFORE it copied it over to the float variables, and now it works

    yay!

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Originally posted by jverkoey

    I then outputted the unsigned int as a %x so i could see the hex form, and it was equal to BF000000....
    On a windows machine? This means, then, that the actual data in the file was:

    0x00 0x00 0x00 0xBF

    because that is how the int "BF000000" is stored.

    Are you certain that the data in the file itself is correct? Why not just use binary streams and the .read() and .write() functions to manipulate the data?

  7. #7
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    yah, i know about the endian order, and i fixed it, so now it works. And about reading from the file, i was going to do that, but then i figured it would probably be best if i read the file in all at once, and then manipulated the data from there. That way, if the program crashes mid-read, it won't lose the file data. It's the *safer* way to do it, and there's not really *that* much of a hit on speed/performance.

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    It sounds, though, like the data in the file itself is in the wrong endian order. Your copy SHOULD work, and it should NOT need you to manually reverse the byte order.

    Also, failures on file read are very unlikely; I doubt you gain much in safety. I would write it such that my read of a file took less than a second, but doing multiple individual reads really is no better or worse than reading all at once and separating out variables later.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM