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 8098599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:07:16+00:00 2026-06-05T22:07:16+00:00

I have the code block below, I deleted all unnecessary parts, just left the

  • 0

I have the code block below, I deleted all unnecessary parts, just left the problematic part. My purpose to fetch the time in a specific format that I need(YYYYMMDDHHmm) by fetch_time function. In order to return char array, I used malloc. However, the program crashes if I run the code below like a minute. When I monitor run-time of the code by a debugging tool, the memory location that p1 points increases e.g 0x72120 for the first iteration, 0x72150 for the second iteration, and so on. So, I suspect it fails because of the memory problem. I wonder what do I do wrong and how can I solve the problem?

Btw, I guess I can solve the problem by defining a global char array and assining the time info in the sub-function. I would like to learn my mistake in the malloc usage and learn the solution. Thank you.

int main(int argc,char *argv[]){
    char timedate2[13];
    char *p1 = malloc(strlen(timedate2)+1);
    if(!p1){exit(1);}

    while(1){
        p1=fetch_time();
    }
}

char *fetch_time() {
    char *p;
    time_t rawtime;
    struct tm * timeinfo;
    char buffer [13];

    time ( &rawtime );
    timeinfo = localtime ( &rawtime );
    strftime (buffer,13,"%04Y%02m%02d%02k%02M",timeinfo);
    p = (char *)malloc(sizeof(buffer));
    strcpy(p, buffer);
    return p;
}
  • 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-06-05T22:07:19+00:00Added an answer on June 5, 2026 at 10:07 pm

    The code looks correct per see. There’s one catch thought. When you call malloc, you are requesting some amount of memory in which you store your date string. Everytime you call fetch_time you will request some (other) piece of memory with a new string. You never re-use the same piece of memory and eventually run out of memory. To fix that, you need to free every piece of memory you requested which will allow the system to eventually reuse it again.

    while(1) {
        p1=fetch_time();
        // do something with p1
        free(p1);
    }
    

    Also, please fix the indentation.

    If you find similar problems in the future, consider using a tool like valgrind, it will help you a lot (google it up!).

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

Sidebar

Related Questions

I have an R code block that generates an image (see below). Executing the
I have a code block that leads to a Internet Explorer cannot display the
I have a code block that checks certain elements before allowing a form to
When i have a code block static void Main() { foreach (int i in
Hi I have following code block public class Driver { static String x =
I need syntax help with the following code logic: I have a code block
I have a small code block that should append text to the beg of
I have the following code block, as the content of my AppDelegate.h of an
I have the following code block for validation in PHP and I can't figure
I have the following markup / code block in the ASPX file. The binding

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.