Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6980117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:58:23+00:00 2026-05-27T17:58:23+00:00

I have a simple log function that needs to print the current date and

  • 0

I have a simple log function that needs to print the current date and time. I’m doing it inside a function that returns a char *. When I try to set this char * into fprintf(), it doesn’t print me the string into the file: why?

Here is the function that constructs the date-time:

char * UT::CurrentDateTime()
{
     char buffer [50];
     time_t t = time(0);   // get time now
     struct tm * now = localtime( & t ); 
     int n=sprintf(buffer, "%d:%d:%d %d:%d:%d:", (now->tm_year + 1900),
                   (now->tm_mon + 1), now->tm_mday, now->tm_hour, now->tm_min,
                   now->tm_sec);
     return buffer;
}

Here is the log:

const char *time =__TIME__; // compilation time 
char *currentTime = UT::CurrentDateTime(); // it's a static method; also tried to set it to const
fprintf(fp, "%s %s %s %s %s %d %s\n", __TIME__, pType, __DATE__,
        currentTime, pFileName, lineNo, pMsg.c_str());
fflush(fp);

Every thing is printed except the date/time char *.
Why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:58:23+00:00Added an answer on May 27, 2026 at 5:58 pm
    char * UT::CurrentDateTime()
    {
         char buffer [50];
         /* ... */
         return buffer;
    }
    

    You’ve returned a pointer to a memory buffer that dies immediately. Any function that uses the pointer returned from CurrentDateTime() is relying upon garbage.

    Your compiler should have warned you about this. Disregard your compiler warnings at your own peril.

    Instead, either allocate this via char *buffer = malloc(50 * sizeof char); or use C++’s memory allocation mechanisms to allocate memory that can live longer than the time the function is ‘live’ and running.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple recursive function that is returning undefined instead of the desired
I have a simple custom error handler that writes in a error log file
I have an issue, where by I am doing a simple form validation, that
I have a simple gateway listener which generates a log at the screen output
I have simple win service, that executes few tasks periodically. How should I pass
I have a powershell script that needs to read from a config file with
I have a simple form that has a list (dropdown list generated from a
I have a function that can return one of three things: success ( True
I have a super simple Backbone model/collection that wraps around a facebook feed. window.Story
I have a simple wpf app which has a button that increments a value

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.