C++ Code Error?

I’m trying to use memcpy to set a vertex buffer for Direct3D but when I send the vertex array, it shuts the program. The memcpy definition asks for a const void * but I am using code that has worked on different machines with a normal void *. I have even gotten it to work before but I just added something that changes the verticies after they have been declared and now it won’t work…

Sample code:
struct D3DVERTEX
{
float x,y,z,nx,ny,nz;
};
VOID *pVertex;
D3DVERTEX cvVertex]={{1,2,3,0.0f,0.0f,0.0f,},};
cvVertex.nx=5.0;
memcpy(pVertex,cvVertex,sizeof(cvVertex));
–>CRASH!!!

I have had the same problem when sending a dynamic string to MessageBox… I think it is a problem with sending dynamic to static but other people have gotten it to work!! Any suggestions?

Well, I found my error… I hadn’t changed the size of my vertex buffer from the previous code. I am still confused about sending dynamic text to a message box though…

Sample:
char *display;
long number=30001;
ltoa(number,display,5);
MessageBox(hWnd,display,“test”,MB_OK);
–>CRASH!!

I don’t see where you get some memory that pVertex can point to. The crash is probably memcpy() trying to blast data somewhere it shouldn’t because pVertex is uninitialized. This may not be the case if you’re doing this outside of the code snippet that you included.

-Kevin

I probably should have included the following line:
m_pVertexBuffer->Lock(0, sizeof(cvVertices), (BYTE**)&pVertices, 0);
Direct3D allows an LPD3DVERTEXBUFFER8 variable to lock a certain amount of memory. This line goes right before the memcpy.

You still need to allocate memory, however. Just because you “lock” the memory doesn’t mean that it exists.

As for your message box thing: you’ve got to allocate some memory for the pointer. You’ve got two options here:

  1. change display from a char * to a char[32]. (or some other sub-script)
  2. allocate some memory via new. Ie. display=new char[32]. If you do this, don’t forget to delete it once you’re done with it.

If you don’t do this, all you’ve got is a pointer to, usually, 0xcdcdcdcd (or something like that), which isn’t valid. When itoa tries to copy text to this location, it’s going to crash because there is no memory there to copy it to.

–Rob

I also forgot to mention that I declared *VOID pVertices at the beginning of the function. I now have all of that working but I can’t get normals to calculate correctly…

*Originally posted by Adam Shapiro *
**
Sample:
char *display;
long number=30001;
ltoa(number,display,5);
MessageBox(hWnd,display,“test”,MB_OK);
–>CRASH!!
**

Adam,

Same problem. “char display" allocates only enough memory to hold the pointer. “char” tells the compiler what kind of data the pointer points to (for type checking). "” tells the compiler to allocate a 32-bit word for the pointer. “display” is a pointer to the pointer that the compiler/linker uses internally. ltoa() blindly uses the address contained in display as the destination address for the move. The problem is that display probably doesn’t point to a valid chunk of memory that you can use. So at run-time, the memory managment hardware in the processor will catch this and return a trap #13 (a/k/a GP fault) to windows. This is a very common mistake in c/c++ coding (yes, I’ve made this mistake more than a few times :)).

Replace the “char *display;” line to “char display[sizeof(long)*8+1];” and it should work.

-Kevin

char *display;
long number=30001;
ltoa(number,display,5);
MessageBox(hWnd,display,“test”,MB_OK);

how to fix this problem?

you can do it Kevin Watson way or you can the other way.

char *display;
long number=30001;

display = 0;
display = new char[sizeof(long)*8+1];
ltoa (number,display,10);
printf("Buffer = %s

", display);

i think when people post a question with code, it should be required that you answer them with code.

http://www.cplusplus.com/ref/cstdlib/ltoa.html
http://www.cplusplus.com/ref/cstdlib/fcvt.html

*Originally posted by Zmeko *
i think when people post a question with code, it should be required that you answer them with code.

:smiley: I agree that getting replies with code is helpful but good explanations such as Kevin’s/Rob’s help as well. Thanks everyone for all of the help. I will be posting a lot more with questions and, once I get it to work, I will post the source/exe’s for “Dasboard3D” on our team website (also under construction at http://www.mvrt.org/team555). My new problem is getting normals to calculate. I have compared my source with the sample I’m working out of and they seem to be in order but the sample works correctly and mine only calculates about 1/3 of the normals…

Just a general tip for C/C++ coding: ALWAYS initialize your variables!

This includes pointers (init to NULL, or zero), integers, strings, etc.

Also, know your API, whether it’s MFC, C runtime, etc. And if you can afford it, use a tool like BoundsChecker from NuMega. It’s great for finding memory leaks, buffer overruns, uninitialize variables, …