Say I have a program that allocates a chunk of memory
char* get_date() {
char* date = malloc(100);
return date;
}
And I want to call the function a considerable number of times in the main function.
int main() {
int i;
for (i = 0; i < 10000; i++) {
char *c = get_date();
//do something
free(c);
}
return 1;
}
How can I reduce the number of times a new chunk of memory is allocated and just allocate one and overwrite it afterwards? Somebody told me about something like this:
char *date = malloc(100);
for (i = 0; i < 10000; i++) {
char *c = get_date(date):
//do something
}
free(date);
But I am not sure how the new function, get_date should look like and why it should work.