Reputation: 1451
I have a memory allocation question. If I declare a char*
within an else
block that means the char*
is destroyed when the else
block is done executing. The else
block is located in a while
loop so it will iterate many times. However, what if the char*
declared in the else
block is aliased to a malloc'd
variable as seen in the below example. My question is how do I fee something like this? I feel as though if I free the temp char*
variable, I will cause a segmentation fault because I will free the variable I want to keep as well. If that is the case, I'm at a loss for where the free
statement goes.
char* print_path = NULL;
(snip)
(while)
else{
char* temp_path = print_path;
int temp_size = strlen(temp_path)+strlen(file_name(child->fts_path))+1;
print_path = (char*)malloc(temp_size);
strcpy(print_path, temp_path);
strncat(print_path, file_name(child->fts_path), strlen(file_name(child->fts_path)));
printf("%s:\n\n", print_path);
}
(snip)
I would like to point out that I do free print_path at the end of the program after I know it will not be executed again. However, it is the intermediary executions of the loop that I would like to free. Any help would be appreciated. Thanks!
Upvotes: 0
Views: 322
Reputation: 1
You have 2 pointers:
print_paths
: Points to an array like ['h','e','l','l','o']
, this pointer contains the address for the first element of the array, in this case it points to h
, lets say that the address of this h
is 1
, so, the address of e
is 2
and so on... hence, print_path
contains the number 1
.
temp_path
: this pointer points to nothing or to a random place in the memory.
When you call malloc
, malloc
ask for some memory and gives you the address of the new allocated memory, but this memory contains garbage.
So after
print_path = (char*)malloc(temp_size);
print_path
contains the address of the new allocated memory, a new array of size temp_size
which has garbage, lets say that this address is 40, so the value of print_path
is 40
.
finally, when you call:
strcpy(print_path, temp_path);
you copy the values of the array temp_path
(['h','e','l','l','o']
) to the array pointed by print_path
, in other words, the address 40
instead of containing garbage now contains h
, 41
contains e
and so on.
It's important to note that, even if both address (1
and 40
) contains the value h
, modifying one doesn't affect the other.
The best place for the free(temp_path)
is just before the end of the else
, just check that temp_path
is allocated memory, if you try to free memory that it wasn't obtained with malloc
your'e gonna have a bad time.
Upvotes: 0
Reputation: 1451
I believe that since, the gist of what I want to do is reallocate memory to the same variable, I should have looked into realloc instead of malloc.
Upvotes: 0
Reputation: 477700
It looks like free(temp_path)
is the right thing. It should go like this:
char * print_path = malloc(...); // "NULL" is also possible
while (condition)
{
if (...)
{
// ...
}
else
{
char * temp_path = print_path;
print_path = malloc(...);
free(temp_path);
}
}
free(print_path);
The invariant in your algorithm is (or should be) that print_path
always points to dynamically mallocated memory. Note how we have precisely one free
for every malloc
.
Upvotes: 2