Copy float value to memory block

This is a discussion on Copy float value to memory block within the C++ Programming forums, part of the General Programming Boards category; I want to copy a float value to an allocated block of memory, with an offset from the memory block ...

  1. #1
    JMK
    JMK is offline
    Registered User
    Join Date
    May 2010
    Posts
    24

    Copy float value to memory block

    I want to copy a float value to an allocated block of memory, with an offset from the memory block pointer.

    Code:
    void Bank::PokeFloat(const int& pos, const float& n)
    {
    	if (pos<0) return;
    	if (pos+4>size) return;
    	memcpy(buf+pos,&n,4);
    }
    How do I do this?
    Last edited by JMK; 09-18-2010 at 01:04 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    Pretty much like what you did.

    Now assuming you tried it, what were your observations and error messages?
    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
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    Why do you want to do it?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    1. For built-in types not greater than the size of a pointer, passing by value is at least as efficient as passing by const reference.

    2. Don't use just 4. Use sizeof(float), for readability sake, if nothing else.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Fragmentation with Dynamic FIFO Queue
    By fguy817817 in forum Linux Programming
    Replies: 17
    Last Post: 10-31-2009, 05:17 AM
  2. Allocate memory inside allocated memory block?
    By Heidi_Nayak in forum C Programming
    Replies: 14
    Last Post: 04-15-2009, 05:19 PM
  3. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 01:05 PM
  4. help me
    By warthog89 in forum C Programming
    Replies: 11
    Last Post: 09-30-2006, 09:17 AM
  5. Multidimensional String
    By Beast() in forum C Programming
    Replies: 14
    Last Post: 07-03-2004, 01:47 AM

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