-
Pointer problem
Hello, I am trying to do a ZeroMemory on a pointer to structure, but it fails on runtime.
Code:
rt3DModel *pModel;
...
...
...
ZeroMemory(pModel, sizeof(rt3DModel));
Debugger shows at the ZeroMemory line (VC++6)
What might be wrong here? I dont use new here.
Thanks
-
If your not using new, then how is that memory allocated?
-
The pModel pointer will be pointing to an invalid address.
You could do:
Code:
rt3DModel pModel; // on stack
...
ZeroMemory(&pModel, sizeof(rt3DModel));
What does zero memory do?
-
ZeroMemory?In what headerfile is this function.
What's the problem?
-
ZeroMemory ammar is a macro of memset.
Ok guys, I created it as new and it worked. But I have a stupid question here:
lets stay i have a structure with several variables, k? And i make an instance of that class:
rt3DModel model1;
rt3DModel model2;
Will both model1 and model2 point to the same values? I mean if model1.something = 5, then will it be model2.something = 5 too (if i set model1.something=5 first)
I think not, but asking to make sure.
-
Of course Not, because when you creat an instance you creat all the variables related to that belonges to it.
Maybe model1.something is equal to model2.something if has a value by default or if you changed it to be equal.
Is that what you mean?
-
No model1 & model2 are seperate entities. model1.something could equal 2, while model2.something could equal 5.
What you could do is this:
Code:
rt3DModel model;
// Extra pointer to model
rt3DModel* modelPtr = &model;
Now modelPtr points at the address of model object. In effect there is only one object (model) plus a pointer to it.
So when you set model.something = 5, when modePtr->something is read, it will be equal to 5 also.
Does this make sense?
-
Ohhh right, so I dont have to update the modelPtr everytime.
Thanks for making this clear :)