|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||||
|
|||||
|
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? |
|
#2
|
|||||
|
|||||
|
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!! Last edited by Adam Shapiro : 01-19-2003 at 12:59 PM. |
|
#3
|
||||
|
||||
|
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 |
|
#4
|
|||||
|
|||||
|
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. |
|
#5
|
||||||
|
||||||
|
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 |
|
#6
|
|||||
|
|||||
|
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...
|
|
#7
|
||||
|
||||
|
Quote:
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 |
|
#8
|
|||
|
|||
|
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\n", 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 Last edited by Zmeko : 01-19-2003 at 04:12 PM. |
|
#9
|
|||||
|
|||||
|
Quote:
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... |
|
#10
|
|||
|
|||
|
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, ... |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| What is your most prefered programming language? | Hailfire | Programming | 156 | 01-19-2005 09:42 PM |
| EMERGENCY! EPROM FULL error?!? | CHSguard72 | Programming | 2 | 03-05-2003 08:51 PM |
| Autonomous code | PBoss | Programming | 7 | 01-14-2003 03:29 PM |
| Does your team use the Default code. | Jeff McCune | General Forum | 2 | 01-09-2003 02:46 PM |
| Error found in programming for the pump and pressure switch | sjharobotics | Programming | 4 | 02-06-2002 05:46 PM |