intro-to-zig-talk/c/raii.cpp

33 lines
521 B
C++
Raw Permalink Normal View History

2024-08-08 18:39:18 +00:00
#include <iostream>
#include <vector>
class MyClass
{
public:
void* ptr;
MyClass()
{
std::cout << "Allocating 1MB\n";
// Allocate 1MB for no reason:
ptr = malloc(sizeof(1000 * 1000));
2024-08-08 18:39:18 +00:00
}
~MyClass()
{
free(ptr);
std::cout << "Destroyed 1MB\n";
2024-08-08 18:39:18 +00:00
}
};
MyClass myClass;
int main()
{
// Trick question, how many times is it constructed and destroyed?
2024-08-08 18:39:18 +00:00
std::vector<MyClass> vecOfMyClasses;
vec.push_back(myClass);
vec.push_back(myClass);
2024-08-08 18:39:18 +00:00
}