How to automatically free heap objects in C++

1 Answer

0 votes
#include <iostream>
#include <memory>

int main() {
    std::unique_ptr<char[]> arr(new char[5]);

    for (int i = 0; i < 5; i++) {
        arr[i] = (char)(97 + i);
        std::cout << arr[i] << " ";
    }
        
    return 0;
}




/*
run:

a b c d e 

*/

 



answered May 15, 2021 by avibootz
...