CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    11

    Angry 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;
    }

  2. #2
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    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.

  3. #3
    Join Date
    May 2001
    Posts
    472
    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.

  4. #4
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    11
    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 ?

  5. #5
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    11
    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

  6. #6
    Join Date
    May 2001
    Posts
    472
    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.

  7. #7
    Join Date
    Aug 2001
    Location
    Italy
    Posts
    60
    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

  8. #8
    Join Date
    Aug 2001
    Location
    North Bend, WA
    Posts
    1,947
    [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;
    }

  9. #9
    Join Date
    May 2001
    Posts
    472
    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
  •  





Click Here to Expand Forum to Full Width

Featured