Advertisement

memcpy and the 'new' operator

Started by January 16, 2001 10:11 AM
4 comments, last by Psychocatt 24 years ago
I am trying to create a 3D file loading function. I have a structure of my vertices. I then dynamically create an array of these to hold the values from the file :
   MYVERTEX *VertArray;

VertArray = new MYVERTEX[nNumVerts];   
After filling in the infromation from the file, I then try to copy my data from the dynamic array to my locked vertex buffer by using memcpy :
   memcpy(&pVertices, &VertArray, sizeof(MYVERTEX) * nNumVerts);   
When I reach this line in my program, it causes a GPF in my program. I assume that this is because I used the new operator and I am just accessing the data area wrong. It does work when I create a static array instead of dynamic, but that seems like a waste to me :
   MYVERTEX VertArray[255];

memcpy(&pVertices, VertArray, sizeof(MYVERTEX) * nNumVerts);     
I have also tried taking out the & from the second parameter, &VertArray in the memcpy, but that also causes problems. I don't know what I am doing wrong. Any help would be much appreciated! Thanks for your time! BTW, sorry about the long post. I just wanted to make sure that I was clear! "I kinda think, therefore, I kinda... am?" Edited by - psychocatt on January 16, 2001 11:18:32 AM
I think you should also take the & out of the first parameter.
Advertisement
Try this:

    Foo *pSource;Foo *pDest;pDest = new Foo[blah];memcpy(pDest, pSource, sizeof(Foo) * blah);    


Doesn't matter whether you use static arrays or dynamically allocate the memory. It's the same thing. (Don't put '&' in before arrays or pointers.)

Jesse Chounard
jesse@k-isilabs.com

Edited by - Jesse Chounard on January 16, 2001 12:02:44 PM
ok. well you should not put the "&" before pointers, because that implies you want to change the value of pointer. i.e.

int i = 0;int j = 255;int *p = &ii = 10; // i = 10, p = (address of "i").(*p) = 20; // i = 20, p = (address of "i")./* ok. */memcpy(p, &j, sizeof(int)); // i = 255, p = (address of "i")./* not ok. */memcpy(&p, &j, sizeof(int)); // ("i" doesn''t change), p = 255.j = (*p); // will cause GPF because p is now an invalid pointer. 


however, placing a "&" before an array probably will not cause an error, because the compiler will understand what you meant (depending on the compiler). array names are constant pointers and there values cannot be changed.

hope this helps.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
Is it just me, or does people on this forum like to repeat what the last poster said??


- code
Don''t put & in front.... eeer... holf on.


---Strange

---Strange

This topic is closed to new replies.

Advertisement