user2520119 user2520119 - 1 year ago 74
C Question

Is memory will be destroyed or not?

Hi i have code like below , just added logic view not full syntax

int globalVar;

static void* average()
void *data = NULL;

if (true)
globalVar = getAverage();
data = ((void *)&globalVar);

return (data);

So my concern is if i return globel variable by assigning to local pointer then will it be in memory or lost when function end?


Answer Source

No there is no memory leak here. Memory leaks only arise from unbalanced malloc and free calls, and there is nothing like that going on here.

(Assuming getAverage() doesn't itself leak).