Sign in to follow this  
mattm

memcpy and offsets

Recommended Posts

mattm    165
Hi, I have a buffer of data that i want to copy several chunks of data into My buffer is large enough to hold lots of chunks and is created as follows: mpVetexStore = new TerVertex [miNumVertices*2* sizeof(TerVertex)]; (it is to hold vertexes so i can batch them beofre placing them into a vertex buffer) I then copy a chunk of data into the buffer, where TerrainVertices is defined as TerVertex* TerrainVertices = new TerVertex[iNumVertices]; and iNumVertices is alot less than the size of the mpVertexStore array. I can copy using: memcpy(mpVetexStore,TerrainVertices,iSizeOfTerVertices); Now what i need to do is advance the mpVertexStore so that when i copy the next chunk it does not overwrite the first. I have tried: mpVetexStore += iSizeOfTerVertices; but this does not seem to have the desired effect. Any help would be greatly appreciated. Thanks, Matt

Share this post


Link to post
Share on other sites
Mercury    776
A few things I see wrong with this code is that here:

mpVetexStore = new TerVertex [miNumVertices*2* sizeof(TerVertex)];

You're creating it as the sizeof( TerVertex ) elements long. (multiplied by a few other things, but that's not important right now) New isn't like malloc in that it doesn't know the size of the type you're creating, you don't need that there. You're wasting a lot of RAM because of that, likely.
We're also going to need to know how you calculate iSizeOfTerVertices to be of any use.

Share this post


Link to post
Share on other sites
Fruny    1658
Quote:
Original post by mattm
mpVetexStore = new TerVertex [miNumVertices*2* sizeof(TerVertex)];


Using new makes taking the size of the element entirely redundant.
You really want mpVetexStore = new TerVertex [miNumVertices*2];

Quote:
I can copy using:

memcpy(mpVetexStore,TerrainVertices,iSizeOfTerVertices);


Use std::copy(TerrainVertices, TerrainVertices+iNumVertices, mpVetexStore);, it will do the Right Thing™ for even non-POD types.


Quote:
I have tried:

mpVetexStore += iSizeOfTerVertices;

but this does not seem to have the desired effect.


I suspect you've left a sizeof in there too... Pointer arithmetic already takes into account the size of the elements.

Share this post


Link to post
Share on other sites
jeroenb    282
What you can do is something like:
memset ( &mpVetexStore[iSizeOfTerVertices], ... )

This code gives you a pointer to element iSizeOfTerVertices in the array. It will then not overwrite any elements before the index iSizeOfTerVertices.

Share this post


Link to post
Share on other sites
jeroenb    282
Yes, forgot to mention that too. The sizeof is not neccessary in the allocation of the memory as the new operator will determine the size of the object at compile time. So make that

mpVetexStore = new TerVertex [miNumVertices*2];

Share this post


Link to post
Share on other sites
mattm    165
Fantastic, thanks alot everyone. I will give this a try and see how it goes.

//edit(2)

Don;t know why i had that sizeof in there either, it wasn't in any of my other declaration! Regardless, I have learnt alot form these few posts alone and would like to say thanks amd that the std::Copy seems to work.I was also been very stupid and forgot to store a copy of the pointer to the first element upon entering the method, hence i lost all my data upon updating mpVertexStore :o

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this