-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

Don
  • 393
  • 3
  • 12

5 Answers5

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" );
John Bode
  • 119,563
  • 19
  • 122
  • 198
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);
Thiago Silveira
  • 5,033
  • 4
  • 26
  • 29
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);
pro_cheats
  • 1,534
  • 1
  • 15
  • 25
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.

Eric Petroelje
  • 59,820
  • 9
  • 127
  • 177
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.

rigby
  • 223
  • 2
  • 10