Click here to Skip to main content
15,889,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I do not make external allocation of arrays or classes because I do not know if it is a right way to do and if the naw allocation of the variable will be available outside the function.
Question: Is right allocate it in an external function?

What I have tried:

This code works in windows:
void external_memory_allocation_function(char **data,int size)
{
	*data=new char[size];
}


void main()
{
	char *data=NULL;
	int size=100000;
	external_memory_allocation_function(&data,size);
	char *data2=new char[size];
	for (int i=0;i<size;i++)
		data2[i]=data[i]=(char) i;
	delete[] data;
	delete[] data2;
}
Posted
Updated 25-Aug-17 1:10am
v3
Comments
Javier Luis Lopez 25-Aug-17 8:17am    
I supose that the memory allocation remains until delete[] operator destroy it.

You can do that, but...
The problem is when it comes to releasing the memory.
Because the allocation is "hidden" in a function, it isn't obvious that it needs to be deleted, and it's quite feasible that the function could be changed to not allocate each time - it could use a global array, or a partial allocation from a bigger memory chunk for example - and then your delete operation will fail. Or same memory could be used by several calls say - and then your delete will get rid of the memory prematurely.

Personally, I'd rather see either "paired" functions, or better the allocation done in the same function that deletes the memory when it's done with.
 
Share this answer
 
Comments
Javier Luis Lopez 25-Aug-17 7:05am    
You are right. I like to hide the variable in a class, allocate it in the constructor and deallocate it in the destructor.
An exception is when you not know the initial size of the variable, as example in a function that reads a file, but you should take care of not use the same variable for other purposes and delete it in the right way.
It does not matter if you allocate in a called function or within the code that would call the function. In both cases the code (more precise: the thread) that triggers the allocation owns the memory and is responsible for freeing it.

Two additional notes:

There is no need to check for a NULL pointer when calling delete. The implementation of delete will do that too and just does nothing when the pointer is NULL.

When deleting an array you should use the delete[] operator:
C++
delete[] data;

You may have also a look at C++ smart pointers:
auto_ptr - C++ Reference[^]
shared_ptr - C++ Reference[^]
unique_ptr - C++ Reference[^]
weak_ptr - C++ Reference[^]
 
Share this answer
 
Comments
Javier Luis Lopez 25-Aug-17 7:09am    
Thanks, I edited delete[] instead delete.
Also I removed the check for null. The initial value of *data=NULL allows using delete[] without error.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900