Thread: Pointer problem

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    132

    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
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    If your not using new, then how is that memory allocated?

  3. #3
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    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?

  4. #4
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    ZeroMemory?In what headerfile is this function.
    What's the problem?

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    132
    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.
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

  6. #6
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    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?

  7. #7
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    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?

  8. #8
    Registered User
    Join Date
    Jun 2002
    Posts
    132
    Ohhh right, so I dont have to update the modelPtr everytime.
    Thanks for making this clear
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer to pointer realloc problem
    By prakash0104 in forum C Programming
    Replies: 14
    Last Post: 04-06-2009, 08:53 PM
  2. Another pointer problem
    By mikahell in forum C++ Programming
    Replies: 21
    Last Post: 07-20-2006, 07:37 PM
  3. Pointer problem
    By mikahell in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2006, 10:21 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. pointer problem
    By DMaxJ in forum C Programming
    Replies: 4
    Last Post: 06-11-2003, 12:14 PM