I have code that looks something like,
#include<stdlib.h>
#include<string.h>
char** someArray = NULL;
size_t numberOfEntriesInArray = 0;
void addToArray(char* someString){
someArray = realloc(someArray, (numberOfEntriesInArray+1) * sizeof(char*));
someArray[numberOfEntriesInArray] = malloc( (strlen(someString) + 1) * sizeof(char) );
strcpy(someArray[numberOfEntriesInArray], someString);
numberOfEntriesInArray++;
}
void deleteSomeArray(){
int i;
for (i = 0; i < numberOfEntriesInArray; i++){
free(someArray[i]);
}
free(someArray);
}
int main(){
addToArray( .. );
..
deleteSomeArray();
}
Is there a way I can know deleteSomeArray has worked properly?
i.e. Is there a way to check if there is still more memory that needs to be freed?
P.S.
If I leak memory in my program, is the memory automatically freed when my program dies? If not, is there a way to get at the leaked memory?
Use a memory debugger. If you are working in Linux (or similar), then the canonical example is Valgrind.
On most modern OSes, yes, the OS reclaims all memory when a process terminates. But you shouldn’t treat this as an excuse for leaking memory!