Reputation: 125
I have a doubt related to freeing up the memory in C program. I don't know how memory gets freed up. below are two scenarios where I am not sure whether I do need to free up memory or not.
below is code snippet
int status=0;
char *grade="high";
status=getPreviousReports(grade, year);
free(grade);
my doubt is that getPreviousReports is doing a soap call and passing grade to it. After that it destroys the soap object in memory, that uses grade in forming soap object.
if I use below code then do I need not to free up grade explicitly ?
status= getPreviousReports("high", year);
Please help me in understanding how and when it is required to free up memory.
Thanks in advance.
Upvotes: 1
Views: 414
Reputation: 23228
As others have noted, in C, if [c][m][re]alloc()
have not been used to create memory, then free()
is typically not necessary. (see also strdup that also requires using free()
)
In a more general sense, all memory in C is said to reside on either the heap or the stack. All of the memory you have created is clearly created on the stack, and does not need to be explicitly freed. Memory created this way is freed as it goes out of scope, or when the executable exits.
Examples of memory created on the heap (then freed) and stack (not freed) include:
int main()
{
int status=0;//created on the stack
char *grade="high";//created on the stack
char *buf = {0};//created here, but no memory yet
char *temp = {0};//ditto
char *dup = {0};//ditto
//....
buf = malloc(10);//on heap if successful. free() now required at some point
if(buf)
{
strcpy(buf, "started..");
temp = realloc(buf, 20);//on heap if successful
if(!temp)
{
free(buf);
return -1;
}
buf = temp;//new memory transferred to buf, free() now required at some point
strcat(buf, "here");
dup = strdup(buf);//on heap if successful, free() now required at some point
if(!dup)
{
free(buf);//memory alloc failed, handle error and leave
return -1;
}
//use buf and dup...
free(buf);
free(dup);
//Note: temp is freed when buf is freed in this example
}
return 0;
}
Upvotes: 1
Reputation: 10440
"Hello" is a String literal. So, it will be there in the memory even if your GRADE variable goes out of scope or you change it to point to somewhere else or it was present even before you declared your GRADE variable.
And free function is used with memory blocks that you have requested during runtime using malloc(), i.e, dynamic memory allocation.
You haven't requested to allocate space for your "Hello" string during runtime and that's why you shouldn't be calling free on it.
Upvotes: 2
Reputation: 41
Memory allocation in C is used by malloc, if memory is not being used (malloc) then there is nothing need to be free.
Upvotes: 4
Reputation: 754
Since you don't allocate (malloc, calloc) any memory you don't need to freeing it. I don't know what exactly getPreviousReports()
is doing. But usually if some function allocates memory it takes care about freeing (but not the passed variables).
Additionally I would recommend you to use debugger and examine *grade after getPreviousReports()
call.
Hope it helps...
Upvotes: 0
Reputation: 28664
If you (or someone else) allocated the memory using malloc
and family, then you free
it, otherwise not. Function which returns memory allocated by malloc
should normally document that for the caller to know.
In your particular case
char *grade="high"
there is no dynamic memory allocation involved, so no need to free
.
my doubt is that getPreviousReports is doing a soap call and passing grade to it . After that it destroys the soap object in memory , that uses grade in forming soap object.
You need more information on what that function is doing internally, and whether it frees some of its parameters manually, e.g grade
, which would be weird.
Upvotes: 3