0

I'm new to C.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void demo() {
    char* s = malloc(10);
    strcpy(s, "foo");
    free(s);
}

int main()
{
    demo();
}

Will this program leak memory with several bytes?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
ntk138
  • 21
  • 4
  • 6
    Is your concern about the last 6 characters of `s`? (past the end of `"foo"`) Worry not, `free` will free the whole allocation of `s`, regardless of how you used it or what you put in it – Alexander Apr 24 '23 at 13:54
  • 1
    `malloc` allocates a little bit of extra memory, and uses it to store the size of the allocation. This is a significant C design mistake, IMHO. – Matt Timmermans Apr 24 '23 at 13:55
  • 1
    It doesn't. Try running it in valgrind, you'll have a message if there is a memory leak. – Yohann Apr 24 '23 at 13:56
  • 4
    Is the actual question here "what is a memory leak"? – Lundin Apr 24 '23 at 13:57
  • 1
    The rule is this: each pointer value returned by `malloc()` or another allocation function must subsequently be passed to `free()`, exactly once, if the allocated memory is to be released before program termination. If your program obeys that rule with respect to every dynamic allocation then it will not leak memory. There is some debate over whether the definition of "memory leak" should allow for unfreed memory if that is still accessible at program termination, but that particular wrinkle is not relevant to your program. – John Bollinger Apr 24 '23 at 15:40
  • @Lundin The actually question is "how does free work" - [This answer](https://stackoverflow.com/a/1957125) describes it: "When you malloc a block, it actually allocates a bit more memory than you asked for. This extra memory is used to store information such as the size of the allocated block ... When you free your pointer, it uses that address to find the special information ..." – ntk138 Apr 25 '23 at 12:07
  • @ntk138 That's one way to implement a heap allocation, but there are others too, such as a pointer look-up table storing address and size. Such an implementation is an access/allocation speed optimization at the cost of memory use. – Lundin Apr 25 '23 at 12:36

2 Answers2

8

Will this program leak memory with several bytes?

No.

free(s) will de-allocate all the memory that was dynamically allocated by char* s = malloc(10); in this example.

As @Alexander commented: "Is your concern about the last 6 characters of s? (past the end of "foo") Worry not, free() will free the whole allocation of s, regardless of how you used it or what you put in it"


Good read: What is a memory leak?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
4

The function malloc is declared the following way

void *malloc(size_t size);

It knows nothing about how the allocated memory will be used and what will be stored in it.

The only parameter size specifies the size of the allocated memory.

To free the allocated memory all you need is to pass the pointer to the allocated memory.

So there is neither memory leak in this code snippet

char* s = malloc(10);
strcpy(s, "foo");
free(s);

The pointer s is point to the allocated memory and this pointer is used to free the allocated memory. And it is entirely unimportant whether something was stored in the allocated memory or not.

If you want to reduce or enlarge the allocated memory when use another standard C function realloc. The function keeps the data stored in the previously allocated memory according to the newly allocated size of the extent of memory.

For example, you could write

char* s = malloc(10);
const char *foo = "foo";
strcpy( s, foo );

size_t n = strlen( s );

char *tmp = realloc( s, n + 1 );

if ( tmp != NULL ) s = tmp;

puts( s );

free( s );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335