3

I created the following function to get Date Time string:

char *GetDateTime (int Format)
{
    if (Format > 2) Format = 0;

    double  DateTimeNow;
    int     BufferLen;
    char    *DateTimeFormat [3] =   {   "%X %x" ,   //Time Date
                                        "%x"    ,   //Date
                                        "%X"    };  //Time
    char    *DateTimeBuffer = NULL;

                        GetCurrentDateTime      (&DateTimeNow);
    BufferLen       =   FormatDateTimeString    (DateTimeNow, DateTimeFormat [Format], NULL, 0);
    DateTimeBuffer  =   malloc                  (BufferLen + 1);
    FormatDateTimeString    (DateTimeNow, DateTimeFormat [Format], DateTimeBuffer, BufferLen + 1 );

    return DateTimeBuffer;
}

I do not free 'DateTimeBuffer' because I need to pass out its content. I wonder if that memory clears itself. Please help.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
CaTx
  • 1,421
  • 4
  • 21
  • 42

5 Answers5

6

It doesn't clear itself. You have to call free in the caller function, or wherever the last access to the memory happens.

Example:

char *dateTimeBuffer = GetDateTime(1);
 .
 . 
 /*  do stuff with dateTimeBuffer */
 .
 .
 /* you don't need dateTimeBuffer anymore */
free(dateTimeBuffer);

Whenever you use malloc you must free manually, but memory allocated on the stack is automatically cleared when you exit the scope in which it lives, for instance in your GetDateTime() function DateTimeFormat will be automatically cleared when the function returns.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • but then how can I pass back the result after 'free'? – CaTx Jan 09 '15 at 18:55
  • @CaTx Where do you want to pass the result after `free` I don't understand your question, you call `free` when you don't need the data anymore. – Iharob Al Asimi Jan 09 '15 at 18:58
  • the 'return' passes back the result, and leave the function. therefore, if 'return' is before 'free', 'free' is never executed. if 'free' is before 'return', the result is lost. how to deal with this problem? – CaTx Jan 09 '15 at 19:01
  • 1
    No, you got it wong... You return a pointer to the memory returned by `malloc` then you have the pointer outside of the function, when you are done with it, then you call `free`. BTW, you should always check that malloc did actually return a valid pointer. Isn't my example clear? – Iharob Al Asimi Jan 09 '15 at 19:04
  • iharob, pardon my ignorance. I did not realize the meaning of the syntax until now. thank you very much. I am new to C. – CaTx Jan 09 '15 at 19:23
  • @CaTx it's Ok, why would you ask if you knew the answer? :) – Iharob Al Asimi Jan 09 '15 at 19:25
  • I did not know. I have been coding w/o fully understanding the meaning. I am sure as of now I still do not understand a lot of things. – CaTx Jan 09 '15 at 19:28
  • @CaTx No I don't mean that, I mean I know you didn't know, that's why you asked. It's not easy to understand this things, specially when you have some background in other programming languages. BTW, didn't you like any of the 5 answers? You haven't accepted any. – Iharob Al Asimi Jan 09 '15 at 19:30
2

In C, nothing ever happens automatically. Every object you malloc, must later be cleared with a free. Since you're returning the DateTimeBuffer from the function, the receiver of the data should process the buffer, and then free it. Be sure to comment this thoroughly for the function.

alk
  • 69,737
  • 10
  • 105
  • 255
Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
  • 1
    Some things happen automatically, others don't. Memory allocated for non-`static` local variables is automatically freed on exit from the function (or from the enclosing block), which why such variables are said to have *automatic storage duration*. Memory allocated with `malloc()` is not released until you explicitly call `free()` or equivalent. – Keith Thompson Jan 09 '15 at 20:14
1

No, every zone allocated with malloc should be explicitly freed by free ; if you don't do that you might have a memory leak. On most OSes, when a process terminates, all its address space is released (so if you don't free memory, it would disappear with its address space)

alk
  • 69,737
  • 10
  • 105
  • 255
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1
char    *DateTimeBuffer

This is pointer local to the function. So when you return from the function the memory allocated will not be freed until you use

free(DateTimeBuffer);

But since the memory is allocated on heap you can return the address of the location which is still valid outside of the function. The memory allocated after being used should be freed explicitly using free()

Gopi
  • 19,784
  • 4
  • 24
  • 36
1

No it doesn't clear.The malloc function will request a block of memory from the heap . You have to pass the pointer returned form malloc to the free function when is no longer needed which deallocates the memory so that it can be used for other purposes.

SVN
  • 254
  • 1
  • 2
  • 6