-3

I want to store the filename path in a char variable and pass it later via function call. To do so, I declared the following char buffer:

char *filename_path = malloc(100* sizeof(char)); 

Now, to test that, I assigned it using a path followed by printing the value of the buffer to make sure it fits well.

filename_path= "../Datasets/Cluster(%d)%s"; printf("%s\n", filename_path); ... free(filename_path); 

However, I get this error :

../Datasets/Cluster(%d)%s k.out(1154,0x7fff9843c3c0) malloc: *** error for object 0x103fbbacb: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug Abort trap: 6 

Can someone hint me where the issue could be? Thank you

5
  • You're only allowed to free pointers returned by malloc. Commented Jul 7, 2017 at 17:48
  • @melpomene Thank you for your answer. Can you please show how to free in this case! Commented Jul 7, 2017 at 17:50
  • 3
    p = malloc(...); p = ... is a memory leak. You're overwriting the pointer returned by malloc. Commented Jul 7, 2017 at 17:51
  • 1
    filename_path= "../Datasets/Cluster(%d)%s"; --> strcpy(filename_path, "../Datasets/Cluster(%d)%s"); Commented Jul 7, 2017 at 17:53
  • @BLUEPIXY I like your way. Very clean! Thank you Commented Jul 7, 2017 at 18:04

5 Answers 5

3

The line

filename_path= "../Datasets/Cluster(%d)%s"; 

does not copy the contents of the string literal to the memory pointed to by filename_path; it overwrites the value of filename_path with the address of the string literal. You're basically throwing away the value returned from malloc, leading to a) a memory leak, and b) the error when calling free.

Instead of using the assignment operator, use the strcpy function:

strcpy( filename_path, "../Datasets/Cluster(%d)%s" ); 
Sign up to request clarification or add additional context in comments.

Comments

1

Your code is overwriting the pointer returned by malloc with a new, constant string (the filename_path = "../Datasets/Cluster(%d)%s" line). It is, then, trying to free it.

You can't do that, because the pointer is not the same as the one returned by malloc anymore.

To copy your contents into your malloc'ed string, you should use strncpy.

Like this:

char *filename_path = malloc(100* sizeof(char)); strncpy(filename_path, "../Datasets/Cluster(%d)%s", 100); printf("%s\n", filename_path); free(filename_path); 

Comments

0

The problem is that you need to save your original pointer in order to free it:

So store a temporary pointer and free that instead.

char *filename_path ,*temp_path; filename_path = malloc(100* sizeof(char)); temp_path = filename_path; //your code for filename_path free(temp_path); 

Comments

0

Look at it this way:

  1. You are allocating 100 bytes of memory and assigning the filename_path pointer to point to that allocated memory.

  2. You now change filename_path to point to the static string "../Datasets/Cluster(%d)%s".

  3. Since you changed where filename_path points to, you no longer have a pointer to the memory you allocated earlier and no longer have any way to free it.

  4. When you attempt to free filename_path you are actually attempting to free the static string "../Datasets/Cluster(%d)%s" which of course as the error message says, was not actually allocated by malloc.

Comments

0

The issue is memory location of malloc() versus a string literal such as filename_path= "../Datasets/Cluster(%d)%s";

malloc() allocates memory on the heap and returns a pointer to that memory. String literals are located somewhere else entirely -- usually in static/data(see this post for more info where in memory are string literals ? stack / heap?)

Since you assigned filename_path to point to somewhere other than where you malloced, you are unable to free it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.