I am not sure why Valgrind is reporting memory leak on it?
Pseudocode in .c file:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* str_alloc(char *str) {
char* dup = (char*) malloc((strlen(str) + 1) * sizeof(char));
strcpy((char*) dup, (char*) str);
return dup;
}
void function_c(char **name) {
int len = 10;
(void) realloc(*name, 100);
}
void function_b(char **name) {
(void) function_c(name);
}
void function_a(char **name) {
(void) function_b(name);
}
int main() {
char* name = str_alloc("");
function_a(&name);
// Do something with name
free(name);
}
I am ignoring the value of realloc since I am kind of sure that realloc will always be on same pointer. Allocating/Reallocating from my own memory pool which is quite huge.
Of course, I can not write the code for memory pool here.
So, In what cases shall I expect to come as Valgrind leak? Couple of cases which I thought of:
- If realloc happens to smaller memory. realloc is guaranteed to take care of it.
- If realloc fails, then it is supposed to be memory leak. I understand this.
Any other cases?