how to delete dynamically allocated memory in c++ code example
Example 1: c++ delete dynamically allocated array
int length = 69;
int * numbers = new int[length];
delete[] numbers;
Example 2: new in c++
#include <iostream>
#include <string>
using String = std::string;
class Entity
{
private:
String m_Name;
public:
Entity() : m_Name("Unknown") {}
Entity(const String& name) : m_Name(name) {}
const String& GetName() const {
return m_Name;
};
};
int main() {
int* b = new int;
int* c = new int[50];
Entity* e1 = new Entity;
Entity* e = new Entity[50];
Entity* alloc = (Entity*)malloc(sizeof(Entity));
delete e;
Entity* e3 = new(c) Entity();