|
-
August 26th, 2002, 11:01 AM
#1
using free function after malloc
Have written a function as follows....I am getting an error when I try to free the allocated memory through malloc...
Have put in a commment against the Free functions that are returning an error
Please HELP!
CString getLogFilename()
{
int x,temp;
char *yyyy = (char *)malloc(4);
char *mm = (char *)malloc(2);
char *dd = (char *)malloc(2);
char *zeroCat = (char *)malloc(2);
strcpy(zeroCat,"0");
CTime t = CTime::GetCurrentTime();
CString filename;
char appendDate[8];
/* get the year in yyyy format*/
x = t.GetYear();
_itoa(x,yyyy,10);
/* get the Month in mm format */
x = t.GetMonth();
_itoa(x,mm,10);
if(strlen(mm) == 1){
strcat(zeroCat,mm);
strcpy(mm,zeroCat);
strcpy(zeroCat,"0");
}
/* get the Day in dd format */
x = t.GetDay();
_itoa(x,dd,10);
if(strlen(dd) == 1){
strcat(zeroCat,dd);
strcpy(dd,zeroCat);
}
strcpy(appendDate,yyyy);
strcat(appendDate,mm);
strcat(appendDate,dd);
filename = appendDate;
free(yyyy); /*Error */
free(mm); /*Error */
free(dd); /*Error */
free(zeroCat); /*Error */
free(appendDate); /*Error */
return filename;
}
-
August 26th, 2002, 11:04 AM
#2
First of all, you have to allocate n + 1 chars for a string of length n, because of the trailing 0 meaning end of string.
-
August 26th, 2002, 11:12 AM
#3
You have not allocated the memory for appendDate with malloc - that is the cause of the crush. You should have used static arrays instead of dynamic memory allocation as it is inappropriate here.
Last edited by Alexey B; August 26th, 2002 at 11:18 AM.
Ce n'est que pour vous dire ce que je vous dis.
-
August 26th, 2002, 01:13 PM
#4
Originally posted by Yves M
First of all, you have to allocate n + 1 chars for a string of length n, because of the trailing 0 meaning end of string.
Thanks Yves....it is working now.
Also is there a way of shortening the whole program using wsprintf ?
-
August 26th, 2002, 01:14 PM
#5
Originally posted by Alexey B
You have not allocated the memory for appendDate with malloc - that is the cause of the crush. You should have used static arrays instead of dynamic memory allocation as it is inappropriate here.
yes Alexei...I have now fixed this problem..and it is working now thanks heaps
-
August 26th, 2002, 01:27 PM
#6
Also is there a way of shortening the whole program using wsprintf?
sscanf or CString::Format can shorten your code a great deal:
Code:
CString getLogFilename()
{
CTime time = CTime::GetCurrentTime();
CString filename;
output.Format("%i/%i/%i", time.GetYear(), time.GetMonth(), time.GetDay());
return filename;
}
Ce n'est que pour vous dire ce que je vous dis.
-
August 26th, 2002, 01:29 PM
#7
you can try this way...
CString getLogFileName()
{
CString strFileName;
SYSTEMTIME st;
::GetLocalTime(st);
strFileName.Format(TEXT("%04d%02d%02d", st.wYear, st.wMonth, st.wDay);
return strFileName;
}
bye,
m_l
-
August 26th, 2002, 02:14 PM
#8
[QUOTE]quote:
--------------------------------------------------------------------------------
Also is there a way of shortening the whole program using wsprintf?
--------------------------------------------------------------------------------
sscanf or CString::Format can shorten your code a great deal:
code:--------------------------------------------------------------------------------CString getLogFilename()
{
CTime time = CTime::GetCurrentTime();
CString filename;
output.Format("%i/%i/%i", time.GetYear(), time.GetMonth(), time.GetDay());
return filename;
}
QUOTE]
Even simpler to use CTime::Format.
Code:
CString getLogFilename()
{
CTime time = CTime::GetCurrentTime();
CString filename = time.Format("%Y/%m/%d");
return filename;
}
-
August 26th, 2002, 02:23 PM
#9
Code:
inline CString getLogFilename()
{
return CTime::GetCurrentTime().Format("%Y/%m/%d");
}
Ce n'est que pour vous dire ce que je vous dis.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|