Pointer problem

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • Pointer problem


      I need some clearity on pointers. Heres the problem with some code.

      Source Code

      1. void CEngine::ResizeFunction( int iWidth, int iHeight )
      2. {
      3. ...
      4. ViewRender->UpdateView( iWidth, iHeight );
      5. ...
      6. }

      Source Code

      1. [CODE]
      2. void CViewRender::UpdateView( int iWidth, int iHeigth )
      3. {
      4. m_View.m_iWidth = iWidth; <-- KABOOM!!!
      5. m_View.m_iHeight = iHeigth;
      6. glViewport( 0, 0, iWidth, iHeigth );
      7. }

      ("m_View" is just a class to hold data about the view)

      if I comment out m_View.m_iWidth = iWidth; and m_View.m_iHeight = iHeigth; then it will not crash. I'm not sure why this crashes, can someone inform me as to why or what I am doing wrong here?


    • RE: Pointer problem

      What's the error? Many times aggregate classes don't properly initialize their member objects and you might be assigning iWidth and iHeight to a non-existent memory location - this would give something along the lines of "unable to write to location 0x00000000" as an error.

      Make sure you explicitly construct your member objects in your aggregate class constructor to avoid that little gotcha (though sometimes this causes me headaches anyway if I declare my main class globally for some reason). For Example:

      Source Code

      1. MyClassConstructor(){
      2. m_dataObject1 = new DataClass1();
      3. m_dataObject2 = new DataClass2();
      4. }

      And you might go ahead and check your assignee before pushing iWidth and iHeight - like so:

      Source Code

      1. if(m_view.m_width)
      2. m_view.m_width = iWidth;

      Hope I've been at least a little helpful.....

      "Your job is not to die for your country. Your job is to make some other poor sod die for his."
    • RE: Pointer problem

      OH GOOD JOB SABRINA!! Wow, I guess it DOES help to create the object BEFORE using it! I found the problem. I was creating my WinApp before creating a ViewRender. Because of this I was calling "ViewRender->UpdateView( ... );" before it was ever created. I don't know why I keep falling into this trap!

    • Don't feel bad, Sabrina. I do that stuff all the time, and it's quite frustrating. I was talking to another Sophmore today (at DigiPen), and he couldn't tell me what a reference was or what const on a class member function did. It made me sad inside.
      Feel you safe and secure in the protection of your pants . . . but one day, one day there shall be a No Pants Day and that shall be the harbinger of your undoing . . .