So I see that there's pairings with memory management functions such as malloc/free
, new/delete
, and new[]/delete[]
, but sometimes you're not always calling new
or new[]
and you still have to call delete
, delete[]
, or free
. For example, strdup is one such function which encapsulates the allocation of memory and you'd still have to deallocate that memory yourself later on.
So when do you know to free memory, if you're not always writing new
or malloc
in some sense? I'm particularly curious about deleting arrays of objects. Just simple arrays. Because, that seems to be one of the use cases that delete[]
is made for. (I'm looking at cplusplus.)
So, is this valid? Is this necessary? Is anything missing?
unsigned char bytes[4] = {0xDE, 0xAD, 0xBE, 0xEF};
for(int i = 0; i < 4; i++)
std::cout << std::hex << +bytes[i];
delete[] bytes;
bytes
is technically an unsigned char *
type, right? I know I technically have a pointer, but because this isn't an array of objects, I'm unsure whether or not it gets automatically destroyed.