-1

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.

Gigi
  • 3
  • 2
  • this is not a homework or anything related... – Gigi Oct 08 '17 at 22:11
  • 4
    Somebody is simply right (if you then remove the `malloc` in `get_date`, of course) – Stephan Lechner Oct 08 '17 at 22:12
  • @StephanLechner If you modify `get_date` by removing `char* date = malloc(100);`. – nbro Oct 08 '17 at 22:14
  • First, `char* void` isn't a thing. Second, you `free()` dynamically allocated memory once you no longer need it. Third, dynamically allocating a date doesn't make a lot of sense, you should make a data type for it and pass it by value. – n. m. could be an AI Oct 08 '17 at 22:15

4 Answers4

1

You are trying to save 10,000 malloc / free calls?

Before you change your code, measure how long it takes. If you are too lazy to measure it, then the speed is not important, so don't change it.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • 1
    This answer is the only one (among other provided) suggests to have a baseline before performance optimisation. Given it is required for every optimisation, why was it downvoted? :-S – zerkms Oct 08 '17 at 22:37
  • 2
    @zerkms: Not the downvoter and you are very right about not doing premature optimisations. Nevertheless, this does not answer the question and should better be a comment to the (unnecessary) question. – too honest for this site Oct 08 '17 at 22:45
  • @Olaf totally. Does not deserve a downvote. But every other question should be prepended with "Make a performance baseline to know whether your change made anything better" – zerkms Oct 08 '17 at 23:03
1

Instead of having get_date return a pointer to the buffer with the data produced by the function, make it take a pointer to a buffer into which the data will be written. I.e. the prototype of get_date could be something like

void get_date(char *buf);

However, it might be useful for get_date() to be able to tell not only the starting address of the buffer but also the size of the buffer . That way, the function can tell if the given buffer is too small (and then return e.g. an int indicating an error code). Hence, the prototype

int get_date(char *buf, size_t len);

might be more useful in practice.

On the caller side, you could then use e.g.

char date[100];
for (i = 0; i < 10000; i++) {
    // return value 0 means success
    if (get_date(date, sizeof date) == 0) {
        //do something
    }
}

I.e. in this case, you wouldn't need malloc or free at all.

Frerich Raabe
  • 90,689
  • 19
  • 115
  • 207
0

100 bytes? Why are you using malloc() at all?

for (i = 0; i < 10000; i++) {

    char c[100]:
    //do something
}
Andrew Henle
  • 32,625
  • 3
  • 24
  • 56
-1

If you have a function that returns temporary fixed size memory, the memory can be static:

thread_local char get_date_buffer [100];

char* get_date() {
    // change get_date_buffer...
    return get_date_buffer;
};

You must not free the return value and you must copy the data when reusing it and recalling the function. Many api-functions use this technique, like glGetString.

cmdLP
  • 1,658
  • 9
  • 19
  • 2
    Be careful with this approach - it's not multithread-safe. – Andrew Henle Oct 08 '17 at 22:43
  • Now, the array should be allocated per thread in the thread local section. It should not matter in this case if you have a single variable or an fixed size array, because the array is just like writing a hundred times ``thread_local char c0, c1, c2, ...``. – cmdLP Oct 09 '17 at 05:20