0

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!

3
  • 2
    You are confusing things that are stack allocated and heap allocated. You are not freeing the pointer, you're freeing the memory that the pointer points to. Commented Sep 27, 2012 at 21:00
  • What's the point of freeing during every iteration of the loop? You know you're going to need that segment of memory for the duration of the loop, freeing and reallocating doesn't make much sense here. Commented Sep 27, 2012 at 21:01
  • @JordanKaye The point of freeing during every iteration is because the previous iteration's dynamically allocated variable is no longer needed. If this is run over a long enough data sample, there will be no memory left. It is also good programing practice to free all malloc'd variables when they are no longer needed. Commented Sep 28, 2012 at 0:27

3 Answers 3

2

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 mallo­cated memory. Note how we have precisely one free for every malloc.

Sign up to request clarification or add additional context in comments.

1 Comment

yes, that is where i originally had the free(temp_path) statement however I am getting a segmentation fault but I thought that I might have been doing something wrong with it because I was almost positive that the free was supposed to go there. Could it be that the temp_path is not actually a dynamically allocated block of memory?
0

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.

Comments

0

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.

2 Comments

Thank you, but that I had tried that before I posted because I had a segmentation fault when I put it there. Could it be that it is because temp_path is a copy of print_path instead of dynamically allocated?
@tpar44 maybe is because your first print_path is NULL, are you trying to free a NULL pointer?, try something like this if (temp_path!=NULL) free(temp_path); if your first print_path is not a NULL pointer, probably you are assigning to temp_path something like temp_path=child->fts_path, i.e. you are assigning to temp_path memory that you are gonna use later, so, you free it in this part of the code, and you try to access it later

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.