Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. new & delete question

new & delete question

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MemLeak
    wrote on last edited by
    #1

    Hi, If I have in my *.h: double** m_GLdouble; and in my *.cpp: m_GLdouble = new double*[m_Size * 12]; when it's time to delete, do I have to do? for(int i = 0 ; i < m_Size * 12 ; i++) { delete m_GLdouble[i]; } delete m_GLdouble; ->or<- delete [] m_GLdouble; m_GLdouble = NULL; Also is there a way to return a pointer such a way that the receiving function cannot modifie the returned object? Something like: Object* myClass::myReadFunction() { return m_Object; } void otherClass::otherfunction() { Object* pObj; pObj = m_myClassObj->myReadFunction(); //Is there a way to make pObj impossible to modify? } hope you understand Thanks Everything's beautiful if you look at it long enough...

    H D 2 Replies Last reply
    0
    • M MemLeak

      Hi, If I have in my *.h: double** m_GLdouble; and in my *.cpp: m_GLdouble = new double*[m_Size * 12]; when it's time to delete, do I have to do? for(int i = 0 ; i < m_Size * 12 ; i++) { delete m_GLdouble[i]; } delete m_GLdouble; ->or<- delete [] m_GLdouble; m_GLdouble = NULL; Also is there a way to return a pointer such a way that the receiving function cannot modifie the returned object? Something like: Object* myClass::myReadFunction() { return m_Object; } void otherClass::otherfunction() { Object* pObj; pObj = m_myClassObj->myReadFunction(); //Is there a way to make pObj impossible to modify? } hope you understand Thanks Everything's beautiful if you look at it long enough...

      H Offline
      H Offline
      Harsha Gopal
      wrote on last edited by
      #2

      The way have allocated memory to the pointers, I suppose that one delete statement should be sufficient. Reg. your second question, may be you can consider using const Object obj; obj=&m_myClassObj->myReadFunction(); Harsha ---------------------------------- http://www.ece.arizona.edu/~hpg ----------------------------------

      1 Reply Last reply
      0
      • M MemLeak

        Hi, If I have in my *.h: double** m_GLdouble; and in my *.cpp: m_GLdouble = new double*[m_Size * 12]; when it's time to delete, do I have to do? for(int i = 0 ; i < m_Size * 12 ; i++) { delete m_GLdouble[i]; } delete m_GLdouble; ->or<- delete [] m_GLdouble; m_GLdouble = NULL; Also is there a way to return a pointer such a way that the receiving function cannot modifie the returned object? Something like: Object* myClass::myReadFunction() { return m_Object; } void otherClass::otherfunction() { Object* pObj; pObj = m_myClassObj->myReadFunction(); //Is there a way to make pObj impossible to modify? } hope you understand Thanks Everything's beautiful if you look at it long enough...

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        For every new, you'll need a matching delete. So if your allocation looked like: double **m_GLdouble; m_GLdouble = new double*[m_Size * 12]; the deallocation would be: delete [] m_GLdouble; But if you also had allocations for each of those pointers:

        for (int x = 0; x < (m_Size * 12); x++)
        {

        = new double;

        }

        you'd have to delete those before deleting m_GLdouble itself. The deallocation would be:

        for (x = 0; x < (m_Size * 12); x++)
        {
        delete m_GLdouble[x];
        }
        delete [] m_GLdouble;

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups