Thread: strftime makes together with calloc an error: realloc(): invalid next size

  1. #1
    Registered User
    Join Date
    Dec 2018
    Posts
    2

    strftime makes together with calloc an error: realloc(): invalid next size

    Hello!

    It would be great if someone could help me with this problem.

    Code:
    #define MAX_ELEMENTS 1000
    #define MAX_ELEMENTS_TIME 40
    
    time_t rawtime;
    struct tm* time_info;
    char buffer_filedate[MAX_ELEMENTS_TIME];
    
    time(&rawtime);
    time_info = localtime(&rawtime);
    int number_of_char;
    number_of_char = strftime(buffer_filedate,MAX_ELEMENTS_TIME,"%d.%B%Y/%H:%M_Uhr", time_info);
    The prog works fine. But as soon as "buffer_filedate"

    Code:
    console_output_string = (char *) realloc(console_output_string, (sizeof(console_output_string)+sizeof(buffer_filedate)));
    strcat(console_output_string,buffer_filedate);
    is attached to an array dynamically the compiler tells me: "realloc(): invalid next size"

    If i change strftime() format, for example to:
    Code:
    number_of_char = strftime(buffer_filedate,MAX_ELEMENTS_TIME,"%d.%B%Y/%H:%M", time_info);
    everything works fine.

    I did not read anything, that this minimal difference (Line 11 of first code-snippet vs Line1 of the third code-snippet) in the function strftime() could be invalid.

    Sorry for my bad english. I try my best to get better!!
    Last edited by PhoenixStefan; 01-29-2019 at 06:24 PM. Reason: wrong language

  2. #2
    Registered User
    Join Date
    Dec 2018
    Posts
    2
    Problem solved!
    The programm used sizeof(console_output_string) multiple times before code-snippet 2. But console_output_string is a dynamically constructed array. And: sizeof() can not return the size of a dynamically allocated buffer. It was not a problem with strftime() directly, the length of the computed format-char only made this problem visible.

    Thank you all for reading.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-13-2013, 11:35 AM
  2. calloc and realloc
    By sebby64 in forum C Programming
    Replies: 1
    Last Post: 12-04-2010, 03:38 PM
  3. realloc(): invalid size:
    By fxtdr79 in forum C Programming
    Replies: 4
    Last Post: 06-03-2010, 09:30 PM
  4. Replies: 3
    Last Post: 08-22-2008, 11:12 AM
  5. Some help with "realloc invalid next size" problem?
    By ninboy in forum C Programming
    Replies: 6
    Last Post: 05-20-2008, 11:57 AM

Tags for this Thread