handmade.network » Forums » Array with void* refereces
khofez
12 posts
#13296 Array with void* refereces
1 month ago

Hi, I would like to know if its possible to have an array of void* and everytime I allocate a memory I would add its pointer to the array of void*, and then in the end of my application I would just go through the list of the void* and deleting every one. Is that possible?

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
//application init//
struct myStruct
{
  int anInt = 3;
  float aFloat = 40.0f;
}

void** pointers = new void*[3];

for(int i = 0; i < 3; i++)
{
  myStruct* ms = new myStruct();
  pointers.push_back(ms);
}

//application loop//

//application end//
for(int i = 0; i < 3; i++)
{
  delete pointers[i];
}

ratchetfreak
315 posts
#13297 Array with void* refereces
1 month ago

If you are relying on destructors running when doing that you are out of luck. Though you don't need to actually deallocate memory yourself like that anyway. Once the application exits the operating system will reclaim all allocated memory.

Also if you are allocating like this then you may as well use a memory arena (VirtualAlloc a large block and override the default allocator to parcel out chunks of it)

khofez
12 posts
#13298 Array with void* refereces
1 month ago

I see, you're right!