I am writing a function which returns an array. In this function I want to do some calculations to give me an integer, which is the size of the array I want to allocate memory to. In the function I therefore use malloc to allocate an array with the specific size and I return this array called returnArray.
However, what happens if I call the function without using the return? Will the allocated memory still be allocated? Is this a very bad problem? I suspect memory leak, but I'm not entirely sure.
char * findValueAndCreateArray() {
int value = 0;
while(something, something..) {
value++;
}
char * returnArray = (char*) malloc(sizeof(char) * value);
return returnArray;
}
findValueAndCreateArray;
Thanks in advance!
freeit, you will create a memory leak. This is not much different from callingmalloc(some_size)and ignoring its return value.