Set struct array free

Posted By: Benni003

Set struct array free - 02/03/14 18:41

Hi I just need to now how to set the memory area free.
I know I can use the free() funktion to do that. But how I have to write for a pointer array?

Here is the code:

Code:
typedef struct GRAPH
{
	POINT **points; // Knoten-Array
	
} GRAPH;

void f_create_graph(var num_points)
{
	// create
	
	GRAPH* graph = malloc(sizeof(GRAPH));
	
	graph.points = (POINT **)malloc(num_points * num_points * sizeof(POINT*));

	// set free
	
	free(graph.points);
	
	free(graph);
}



I think free(graph.points); is incorrect to set the complete pointer array free, or is it right?
Posted By: WretchedSid

Re: Set struct array free - 02/03/14 18:46

It is. You balance out malloc() calls with free() calls.

You are also not explicitly allocating an array, at least not as far as the OS, CPU, TLB or RAM is concerned. All you do is request a contiguous block of memory.
Posted By: Benni003

Re: Set struct array free - 02/03/14 18:49

Thank you, also for your fast answer laugh
© 2024 lite-C Forums