C ++ memory and vector management

I was very confused in managing memory with respect to vectors and could do with an explanation of some basic concepts.

I have a program that uses large vectors. I created the vectors using the new operator and released them at the end of the program with delete to return the memory.

My question is: if the program crashes or is interrupted for any reason, the delete lines will be skipped, is there a way to restore memory even in this scenario.

I also have other large vectors that I assign without the new keyword . I read that they will be created on the heap, but should not be freed up anyway, as memory management is handled "under the hood." However, I'm not sure if this is the case, because every time I run my program, I lose RAM.

So, my second question is: can vectors created without the new keyword really stay on their devices and trust in order to clear them after themselves, even if the code is interrupted in the middle of the stream.

And I believe that the third question that has just arisen in my head is that if Vectors are automatically created in a bunch, why would you use the new keyword with them? Thanks for reading, Ben.

+5
source share
8 answers

I suspect your questions are about std :: vector <T> (unlike the array T []).

  • When your application crashes or is interrupted for any reason, the OS restores memory. If you are not using a really rare OS and you find an error.
  • , , . , , , , ( , - ). , , , ( , , , ), , .
+13

new . .

- . . , , , , . . :

class HeapInt
{
    public:
        HeapInt(int i) {ptr = new int(i);}
        ~HeapInt() {delete ptr;}
        int& get() {return *ptr;}
    private:
        int* ptr;
};

int main()
{
    // this code DOES NOT leak memory
    std::vector<HeapInt> vec;
    for (int i = 0; i < 10; ++i)
    {
       HeapInt h(i);
       vec.push_back(h);
    }
    return 0;
}

main() , . :

int main()
{
    // this code though, DOES leak memory
    std::vector<int*> vec;
    for (int i = 0; i < 10; ++i)
    {
       int* ptr = new int(i);
       vec.push_back(ptr);
    }
    // memory leak: we manually invoked new but did not manually invoke delete
    return 0;
}
+8

, .

. , . , , -, . , . , XML , .

, , - , :

  class CLock
  {
  public:
      CLock() {}
      ~CLock() {}

      void Lock(...) {...}

      void Unlock(...) {...}
  };

  std::vector<CLock> myLockVec;

CLock , , ? , .

, :

 std::vector<int*> myIntVec;

, , NULL'd, ? , 0xdeadbeef, .

- , , - - . , , , .

, HOLDS . RAII - - , , - . CLock , , !

 class CLock
 {  
      ...
      ~Clock()
      {
          if (locked)
          {
              Unlock();
          }
      }
 } 

. , smart_ptr. .

class CSmartPointer<T>
{
      CSmartPointer( T* rawPtr)
      {
         m_ptr = rawPtr;
      }

      ~CSmartPointer()
      {
         delete m_ptr;
      }
}

, , .

+4

, , . , .

" , , RAM" - - ?

"" - :

  • , .
  • , .
+3

, std::vector, .

  • std::vector , . , .
  • , , .
+3

.

std::vector, . , push_back(). - , reserve(). , .

std::vector, . . , delete. , .

+2

" ", @RichieHindie.

:

, NEW, ,

( ) , ( , ), , ), - , kill -9 , - - .

+1

, , "", , - . NULL , , ; , , , , 4- , .

+1

All Articles