Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading and Editing Memory Buffers

Status
Not open for further replies.

TheObserver

Programmer
Mar 26, 2002
91
US
I have two memory buffers. I need to read some data from one (in a specific location) and place it into the second one (in a specific location). I know where these locations are (offsets from start, size of the locations to update in bytes, etc)...however, I haven't really done much with memory buffers, so I was wondering if anybody could tell me about how to read from and write to memory locations, or point me at some easily grasped resources for such.

BTW, these "locations" will vary in size from one to another...IE, I'll need to read in 8 bytes and place it in the target buffer, then read in 30 bytes and place it somewhere else in the target buffer, etc.

Thanks for your time.
 
Let's say these are your buffers:

char *srcbuf;
char *destbuf;

And you want to copy 10 bytes from srcbuf starting at the 15th byte into destbuf starting at the 8th byte. You would do the following:

memcpy(&destbuf[7],&srcbuf[14],10);


Dennis
 
Dennis has explained it all. Just to add a bit, in case your source and destination buffers are overlapping, use memmove() instead of memcpy().
 
My parameters are actually received as:
myfunction(char *srcbuf, char *&destbuf)

I'm doing this:
memcpy(&destbuf[0],&srcbuf[0], srcbuffersize);

This memcopy ultimately ends up being stored in a text file.


No matter if I have the srcbuf as the destination or the source, the destination buffer ends up in the text file unupdated with the information from the source.

Any ideas? Thanks for your replies.

 
Hi, Buddy, You got your buffer unupdated is right. because you really don't want to change the buffer content, your function signature seems like that!

Here is your function signature:
myfunction(char *srcbuf, char *&destbuf)

Pay attention to the second parameter: char *&destbuf!!!

First, & means you want to pass reference to the function,
Second, you add * to tell the compiler you want use the data in the (&destbuf). so it is just equal to char destbuf.

So that won't change the content of the destbuf out of myFunction. But if you check the destbuf inside the myFunction, it might be changed.

You probably should check your function signature.

jamessinew@yahoo.ca
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top