Search
 
SCRIPT & CODE EXAMPLE
 

CPP

c++ garbage collection

// raii.cpp

#include <iostream>
#include <new>
#include <string>

class ResourceGuard{
  private:
    const std::string resource;
  public:
    ResourceGuard(const std::string& res):resource(res){
      std::cout << "Acquire the " << resource << "." <<  std::endl;
    }
    ~ResourceGuard(){
      std::cout << "Release the "<< resource << "." << std::endl;
    }
};

int main(){

  std::cout << std::endl;

  ResourceGuard resGuard1{"memoryBlock1"};

  std::cout << "
Before local scope" << std::endl;
  {
    ResourceGuard resGuard2{"memoryBlock2"};
  }
  std::cout << "After local scope" << std::endl;
  
  std::cout << std::endl;

  
  std::cout << "
Before try-catch block" << std::endl;
  try{
      ResourceGuard resGuard3{"memoryBlock3"};
      throw std::bad_alloc();
  }   
  catch (std::bad_alloc& e){
      std::cout << e.what();
  }
  std::cout << "
After try-catch block" << std::endl;
  
  std::cout << std::endl;

}
Comment

is there garbage collection in c++

A C++ program can contain both manual memory management and garbage collection happening in the same program. According to the need, either the normal pointer or the specific garbage collector pointer can be used. Thus, to sum up, garbage collection is a method opposite to manual memory management.
Comment

PREVIOUS NEXT
Code Example
Cpp :: cpp compare strings 
Cpp :: 83. remove duplicates from sorted list solution in c++ 
Cpp :: conversion of class type data into basic type data in c++ 
Cpp :: how to compile c++ code with g+ 
Cpp :: how to show constellations in starry night orion special edition 
Cpp :: ifstream file (“code2.txt”); dev C++ 
Cpp :: gcd of two numbers 
Cpp :: waiting in a serial as the spool reflect the queue operation. Demonstrate Printer Behavior in context of Queue.Subject to the Scenario implement the Pop and Push Using C++. 
Cpp :: c++ how to do a pointer char to take varols from keyboard 
Cpp :: prefix using stack 
Cpp :: taking integer input from file in c++ 
Cpp :: object inside class c++ 
Cpp :: c++ round number to 2 decimal places 
Cpp :: c++ error missing terminating character 
Cpp :: C++ Relational Operators 
Cpp :: glUniform bool 
Cpp :: get range sum 
Cpp :: c to c++ code converter 
Cpp :: function param pointer to struct prototype in c 
Cpp :: . Single-line comments start with two forward slashes (//). 
Cpp :: CPPDEVELOPER 
Cpp :: Qt asynchronous HTTP request 
Cpp :: 1822. Sign of the Product of an Array leetcode 
Cpp :: code::block uncomment 
Cpp :: DMA c/c++ 
Cpp :: c++ thread id 
Cpp :: add two constant char pointers c++ 
Cpp :: Browse Folder Dialog, Search Folder and All Sub Folders using C/C++ 
Cpp :: xor in c++ 
Cpp :: qt c++ thread example 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =