Jump to content
  • Advertisement
Sign in to follow this  
thestien

void pointer addition MemoryCopy

This topic is 2341 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi Guys im back with more problems from my new book...

in the book it makes a Line drawing class which looks ok but the render function it uses doesnt work i tried messing around with it but still no joy.

i think it is because of the MemoryCopy function ill show the rough code.


void* pData = 0;

r = pVB->Lock( 0, 0, (void**)&pData, 0);
if( FAILED(r))
{
pVB->Release();
return E_FAIL;
}
CopyMemory( pData, (void*)&m_StartPoint, sizeof( CZenVertex));
CopyMemory( pData + sizeof(CZenVertex), (void*)&m_EndPoint, sizeof( CZenVertex));

pVB->Unlock();

more code to draw primitive here...


the problem is it dont know how to count from the void pointer address?

there is some code missing but hopefully from this snippet you can understand what im trying to do and how i should be doing it smile.png
im not sure why the book makes void* i think its needed as a void** in another function before the memorycopy


many thanks in advance smile.png


EDIT: im sorry what i need is a way to find the address of pData and add the sizeof(Vertex) to that as the starting point to copy the Endpoint Vertex into the Vertex Buffer the start point goes in there easy enough though smile.png Edited by thestien

Share this post


Link to post
Share on other sites
Advertisement
You are copying memory into an unallocated pointer, unless CopyMemory is doing the allocation, this is a big bad no-no. Assuming that CopyMemory in question is this CopyMemory it certainly *doesnt* allocate the memory for you.

As to a counting from the void pointer address, it's as simple as pData+4 for example, this would return the position 4 bytes past the point that pData points at. Unliked typed pointers though, you can't assume the size of a void pointer, as a void pointer is sizeless.


Truth told, unless you have a very good reason, you shouldn't really be using void pointers.


As to finding the address of pData, since it is a pointer, it IS the address address in memory. That said, if you want to assigned a pointer, to a pointer to point to a pointer, it works like the following:

int p = 42;
int * pointerToP = &p;
int ** pointerToPointerToP = &pointerToP;



As to your wanting to copy data to a buffer, there are much simpler ways that do not involve pointers to pointers, without seeing the exact code you are working with there isn't much we can help with. Specifically what exactly are pVB, m_EndPoint, m_StartPoint, what is the error you are getting, and is CopyMemory just the Win32 api call?

Share this post


Link to post
Share on other sites
It seems as if some sort of rendering context is mapping pData to some sort of data block in the locking process, as if it is copying a hardware buffer to memory for modification, to be copied back, judging by passing a pointer to pData earlier. Thus, it likely now points to valid data that shouldn't be referenced after the context is unlocked.

As for incrementing a void pointer, it might be taboo based on whatever rules you follow, but the easiest way to increment a pointer is to cast it.

CopyMemory( (void*)((char*)pData + sizeof(CZenVertex)), (void*)&m_EndPoint, sizeof( CZenVertex));

Share this post


Link to post
Share on other sites
pData is allocated by the Lock() call, so this is fine.

What the OP probably wants is:

BYTE* pData = 0;


The problem is that adding an integer to a pointer adds X elements, not X bytes. Because pData is a void pointer, it's size is unknown. If you use a BYTE pointer instead, then the size of an element is 1 byte.

You could also use a CZenVertex pointer instead, and move forwards by 1 element.

Share this post


Link to post
Share on other sites
Wow thanks for the replies guys its got my code Sort of working :)

i would post the entire code but its 2k+ lines so far and very messy the code above is fixed with the BYTE* what i wanted to do is at the address of pData copy one vertex worth of data then from the end of that data copy the last vertex data.

it is very strange i managed to fix this but my code is so messed up it still doesnt work as intended but that is for another topic :)
my code is one file at the moment and i am going to split it up into seperate files to make it more readable for me and anyone hopeing to help me.

im sure in a few weeks ill look back at this thread and realize how much of an idiot i am lol

thanks for the help guys much appreciated :)

Share this post


Link to post
Share on other sites

Evil Steve nailed it, so the only thing worth adding is: "void *pData = NULL;" not "0".

0 Is perfectly valid in C++ and evaluates to the null pointer value.

Share this post


Link to post
Share on other sites
In fact NULL is a non-standard and potentially not-entirely-portable macro that evaluates to 0 on many platforms (chiefly MS Windows).

You should be using nullptr and a C++11 compliant compiler :-P

Share this post


Link to post
Share on other sites
NULL is standard - just what it exactly evaluates to is implementation defined. All you know about it is that it can be used as a null pointer constant.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!