In C, the malloc() and free() function allows dynamic memory management. However, when using this function with char string (char*), sometimes it is easy to forget padding '\0' after the string:
int main() {
size_t i;
char *str1;
for (i = 0; i < 3; i++)
{
str1 = (char*)malloc(10 * sizeof(char));
memset (str1, 'a', 9-i);
printf("ADDRESS:0x%x DATA:%s\n", str1, str1);
free(str1);
}
return 0;
}
And the output might be something like:
ADDRESS:0x4701e0 DATA:aaaaaaaaa
ADDRESS:0x4701e0 DATA:aaaaaaaa►
ADDRESS:0x4701e0 DATA:aaaaaaaa►
The reason for that whenever free(pointer) is called in the C program, the space pointed by the pointer is collected back in the free mem list which can be reused later by another malloc call. So depending on the mechanism of free(), there is a chance that the content in the part of the memory to which a pointer previously points, was not erased after calling free() (ref here). Then we can see the previous content in the pointer, if no '\0' was padding to the end of str1.
Normally, I choose to add memset after malloc, or pad a '\0' to the end, or use calloc instead, e.g. in the loop:
str1 = (char*)malloc(10 * sizeof(char));
// Option 1. Add memset to set the init values
//memset (str1, '\0', 10);
// Option 2. Use calloc() to init to NULL instead
//str1 = (char*)calloc(10, sizeof(char));
memset (str1, 'a', 9-i);
// Option 3. Add the '\0' to the end
//*(str1+9-i) = '\0';
printf("ADDRESS:0x%x DATA:%s\n", str1, str1);
free(str1);
Then the result will be good:
ADDRESS:0x4701e0 DATA:aaaaaaaaa
ADDRESS:0x4701e0 DATA:aaaaaaaa
ADDRESS:0x4701e0 DATA:aaaaaaa
The GNU C strptime function is used to convert a given string (s), which is described as format, into a standard C time struct called tm:
char *strptime(const char *s, const char *format, struct tm *tm);
This time function is very handy when you try to convert a DateTime string into a time struct in C. And is often used together with its counterpart, strftime, to perform translation of the input DateTime string in the original format into the target format (e.g. here).
Recently, when working with the program using VS 2005 compiler, I found that the strptime function was not supported in the Windows' compiler. When checking out the time.h header file in \VC\include\time.h in the Visual Studio folders, you won't see the declaration of the strptime. So in order to use this function on Windows:
1. Since the glibc is designed for UNIX-like system, and it is advisable to use a gcc compiler on Windows (e.g. cygwin) to compile your program;
2. Or use other implementations instead:
1) http://plibc.sourceforge.net/doxygen/strptime_8c-source.html
2) http://www.opensource.apple.com/source/lukemftp/lukemftp-3/lukemftp/libukem/strptime.c
3) (In C++, Boost C++ Libs) http://www.boost.org/doc/libs/1_47_0/doc/html/date_time/date_time_io.html
3. Or write your own conversion function strptime().