Note the following code:
1
2
3
4
5
6
7
8
9
10
11
12 | struct Foo
{
int abc = 2;
float def = 3.0f;
double ghi = 45.0;
};
unsigned char* memory = (unsigned char*)malloc(sizeof(Foo));
Foo* foo = new (memory) Foo();
free(memory);
|
I know that foo will be inacessible because I freed the memory that was used on the new operator, but should I have to use 'delete' on anything that I use 'new' ? is it mandatory? Or should I use new and delete on the memory pointer as well ?
Also if anyone has any alternative to an allocation that brings the initialized values I wrote directly on the struct I would appreciate, because I don't know if the new operator does anything else other than just initializing the values with the custom memory block I passed to it
PS.: I'm just using the new operator with a custom memory on the Foo struct pointer because it takes into account the values initialized directly on the struct, using a zeroed memory block with malloc it will just have zero values, and I want to initialize the values on the struct.