Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: c++ new/delete

  1. #1
    Join Date
    Mar 2008
    Location
    Nottingham
    Beans
    167
    Distro
    Ubuntu

    c++ new/delete

    I've been trying to find an answer by googling it but no luck so maybe someone can help me with my simple memory allocation problem in c++.

    In my the constructor of a class i define the following

    Code:
    int ***_lattice;
     assert(_lattice ==NULL);
        cout<<"Initialising 3D lattice space"<<endl;
        _lattice = new int**[size];
        for(int i = 0; i<size; i++)
        {
           _lattice[i]= new int*[size];
        
            for (int j =0; j<size; j++)
            {
                _lattice[i][j] = new int[size];
            }
        }
        assert(_lattice != NULL);
        cout<<"3D lattice initialised"<<endl;

    and in my destructor i want to dallocate the memory by doing this:

    Code:
         for(int i = 0; i<size; i++)
            {
                for (int j = 0; j<size; j++)
    
                {
                    delete[]_lattice[i][j];
                }
                delete [] _lattice[i];
            }
            delete [] _lattice;
        }
        assert(_lattice==NULL);
        {
            cout<<"lattice deallocation failed"<<endl;
        }
    am i right in assuming that _lattice should be 0 after deallocation? why does my assert keep failing.
    I am a bit lost.

    Thanks for any help
    alea iacta est

  2. #2
    Join Date
    Oct 2007
    Beans
    1,914
    Distro
    Lubuntu 12.10 Quantal Quetzal

    Re: c++ new/delete

    Quote Originally Posted by abraxas334 View Post
    am i right in assuming that _lattice should be 0 after deallocation? why does my assert keep failing.
    No, delete[] does not change the value of the pointer, it just deallocates the memory. If you want the pointer to point to NULL, you have to do that manually afterwards.

    EDIT: Btw, in C++, there is no way to tell whether a pointer points to a valid memory location or not (except that NULL is never a valid memory location)

  3. #3
    Join Date
    Mar 2008
    Location
    Nottingham
    Beans
    167
    Distro
    Ubuntu

    Re: c++ new/delete

    Ok sorry for being slow here, but another delete question:

    if i define an array like this in a class header
    Code:
    class cfoo{
    int *contacts[2];
    };
    
    class cfoo2 : public foo{
    
    } ;
    and call it in a function of a different class like this:
    PHP Code:
    class another
    {
        
    cfoop_cfoo;
        
    cfoo2p_cfoo2;
        
    int someNumber;
        
    void function();
    };

     
    void another::function()
    {
        
    p_cfoo2 = new p_cfoo2;
        
    p_cfoo p_cfoo2;

    //and then
        
    p_cfoo2->contacts = new int [someNumber][2];


    my question now is how do i delete the allocated memory in this case? I tried to delete it in the destructor of cfoo as well as cfoo2 but vlagind is never happy with it and i am still not deallocating all memeory.
    Can anyone help?
    Thanks
    Last edited by abraxas334; November 18th, 2008 at 11:32 AM.
    alea iacta est

  4. #4
    Join Date
    Sep 2007
    Location
    Cambridge, MA
    Beans
    635

    Re: c++ new/delete

    You should check out
    scoped_ptr
    and
    scoped_array

  5. #5
    Join Date
    Oct 2007
    Beans
    1,914
    Distro
    Lubuntu 12.10 Quantal Quetzal

    Re: c++ new/delete

    Quote Originally Posted by abraxas334 View Post
    my question now is how do i delete the allocated memory in this case? I tried to delete it in the destructor of cfoo as well as cfoo2 but vlagind is never happy with it and i am still not deallocating all memeory.
    I double that the example above compiles (Capitalisation is different). Please make sure that you paste the code you've actually tried 1-to-1. I suspect that the compiler complains about something else as well -- namely that your assignment is invalid.

  6. #6
    psusi is offline Ubuntu addict and loving it
    Join Date
    Sep 2005
    Location
    Orlando, FL
    Beans
    3,980
    Distro
    Ubuntu Development Release

    Re: c++ new/delete

    lattice is a local variable, and as such, it's initial value is completely undefined. Usually it will be whatever garbage was left over in that location on the stack, so your comparison to NULL fails.

  7. #7
    Join Date
    Mar 2008
    Location
    Nottingham
    Beans
    167
    Distro
    Ubuntu

    Re: c++ new/delete

    The second example i gave (my second post) does compile and run in my actual code, i just simplified the class and function a bit so any syntax errors are not what i am looking for. I ran my working through valgrind and it gives me an error, that 2 blocks are not freed, and when looking at the line it gives me in the code it comes down to the dynamical assignemnt of the contacts array.
    So how do i free that memory, so i am not leaking memory any more?



    mmmmhhhhh new plan, ill just use a garbage collector for c++, saves me having to work out all the memory allocation/freeing stuff, as i obviously don't quite understand it
    Last edited by abraxas334; November 18th, 2008 at 12:44 PM.
    alea iacta est

  8. #8
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,270
    Distro
    Kubuntu

    Re: c++ new/delete

    Quote Originally Posted by abraxas334 View Post
    The second example i gave (my second post) does compile and run in my actual code, i just simplified the class and function a bit so any syntax errors are not what i am looking for.
    Are you sure? I have not attempted to compile your code, but this statement looks suspicious:
    PHP Code:
    ...
        
    p_cfoo2->contacts = new int [someNumber][2];
    ... 
    'contacts' is a private member of cfoo, thus I doubt you would have access from a function defined within the class another.

    Quote Originally Posted by abraxas334 View Post
    I ran my working through valgrind and it gives me an error, that 2 blocks are not freed, and when looking at the line it gives me in the code it comes down to the dynamical assignemnt of the contacts array.
    So how do i free that memory, so i am not leaking memory any more?
    I did not test the code in your most recent posting, but the code you supplied in the OP appears correct *minus* the check with assert(). I ran a similar test through valgrind (using --leak-check=full option) and it reported no leaks.
    PHP Code:
    template <class T>
    class 
    Lattice
    {
      public:
        
    Lattice(unsigned int size 2)
          : 
    m_size(size)
        {
          
    m_lattice = new T**[m_size];

          for (
    unsigned int r 0m_size; ++r)
          {
            
    m_lattice[r] = new T*[m_size];

            for (
    unsigned int c 0m_size; ++c)
            {
              
    m_lattice[r][c] = new T[m_size];
            }
          }
        }

        ~
    Lattice()
        {
          for (
    unsigned int r 0m_size; ++r)
          {
            for (
    unsigned int c 0m_size; ++c)
            {
              
    delete [] m_lattice[r][c];
            }

            
    delete [] m_lattice[r];
          }
          
    delete [] m_lattice;
        }

      private:
        
    unsigned int m_size;
        
    T***         m_lattice;
    }; 
    Quote Originally Posted by abraxas334 View Post
    mmmmhhhhh new plan, ill just use a garbage collector for c++, saves me having to work out all the memory allocation/freeing stuff, as i obviously don't quite understand it
    C++ garbage collector?

  9. #9
    Join Date
    Mar 2008
    Location
    Nottingham
    Beans
    167
    Distro
    Ubuntu

    Re: c++ new/delete


    appears correct *minus* the check with assert().
    yes i have got rid of that assert as it is pointless, but i did not realise that before.

    contacts' is a private member of cfoo
    actually it is public, i just missed out the public, when i copied and pasted obviously.

    C++ garbage collector?
    Yes there is a library written by Hans Boehm, which in the end managed to solve all my problems (Am a java girl and was only recently forced to do c++ stuff and therefore not used to having no garbage collector i.e having too free memory manually).
    Check it out it really can save alot of time and my whole code is leak free now
    http://www.hpl.hp.com/personal/Hans_Boehm/gc/
    and ubuntu kindly got it installed already
    Thanks for all the help
    alea iacta est

  10. #10
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,270
    Distro
    Kubuntu

    Re: c++ new/delete

    The C++ GC looks interesting, but the use of uppercase letters for the API functions look horrendous.

    I decided to brush up a little on my C++/Boost skills, so I took on Cracauer's suggestion at using shared_array.

    I do not know if I did it correctly; unfortunately I can't get it to release all of the memory. I'll keep looking into it.

    PHP Code:
    #include <boost/shared_array.hpp>
    #include <stdexcept>
    #include <vector>
    #include <iostream>


    template <class Tsize_t S>
    struct Matrix
    {
      
    Matrix()
      {
        
    m_matrix.resize(S);

        for (
    size_t i 0S; ++i)
        {
          
    m_matrix[i].resize(S);
        }
      }
      
    std::vectorstd::vector<T> > m_matrix;
    };


    template <class Tsize_t S>
    class 
    Lattice
    {
      public:
        
    Lattice()
          : 
    m_lattice(new Matrix<T,S>[S])
        {
        }

        
    void insertValue(size_t rowsize_t colsize_t depth, const T value)
        {
          if (
    row >= || col >= || depth >= S)
            throw 
    std::range_error("index out of range.");

          (
    m_lattice[row]).m_matrix[col][depth] = value;
        }

        
    T getValue(size_t rowsize_t colsize_t depth)
        {
          if (
    row >= || col >= || depth >= S)
            throw 
    std::range_error("index out of range.");

          return (
    m_lattice[row]).m_matrix[col][depth];
        }

      private:
        
    boost::shared_arrayMatrix<T,S> > m_lattice;
    };


    int main()
    {
      
    Lattice<int3lattice;

      try
      {
        
    lattice.insertValue(1,1,1,10);

        
    std::cout << lattice.getValue(1,1,1) << std::endl;

        
    lattice.insertValue(4,1,1,10);
      }
      catch (
    std::exceptionex)
      {
        
    std::cerr << "Exception: " << ex.what() << std::endl;
      }

      return 
    0;

    UPDATE:
    I found the problem. I mistakenly used reserve() when attempting to pre-size the vectors. I should have been using resize(). Now the memory leaks are cleared up.
    Last edited by dwhitney67; November 18th, 2008 at 08:09 PM. Reason: Found the memleak bug.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •