Novice wrote:
When Main calls a function, which has a couple of malloc() statements, and
their corresponding free() statements, why did it crash?
How should I fix this problem?
Look carefully. Crashes in malloc/free are a result of some undefined
behavior corrupting the malloc internal tables. Most likely reasons
are:
1. Freeing a value not directly obtained from malloc.
char* x = static_cast<char*>(malloc(10));
x++;
free(x);
or
char x;
free(&x);
2. Freeing a value twice.
void* p = malloc(20);
free(p);
free(p);
3. Writing off the end of a malloc'd bloc
char* x = static_cast<char*>(malloc(4));
strcpy(x, "abcd");
Some compilers have debug tools built in to help you troubleshoot this
and there are some external third party tools available. However,
usually so serious introspection of your code will help.
Frankly, if you are a new C++ programmer (or perhaps even an old one),
the fact that you are messing with malloc at all means you have serious
problems in your design approach.