Regarding global variables memory allocation and gtime funtion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sunil

    Regarding global variables memory allocation and gtime funtion

    Hi,

    Am developing one shared library for one application.
    In that .so am having one global array of structure ( two more
    different structure pointers in this struct).
    Once the application is launched, then I am allocating the memory on
    the heap for the internal structures.

    To print the log messages I am using the below mentioned global
    variables and time header functions.

    variables
    static time_t ltime;
    static char timeString[TIMESTAMP_SIZE];
    static struct tm *newtime;

    functions
    time(&ltime);
    newtime = gmtime(&ltime);
    memset(timeStri ng,'\0',TIMESTA MP_SIZE);
    strftime(timeSt ring, TIMESTAMP_SIZE, "%Y-%m-%d-%X%z------",newtime);

    And am using the above timestring variable to print the timestamp.

    It is working fine on unix boxes.
    It is failing in some boxes after printing some log messages.
    After executing the gmtime function, the values of the internal
    structures are becoming null.
    And the program is trying to access the null values and it is failing.


    If I make the above time related variables to static(earlier the
    variables are global) then the program is executing fine without any
    problems.

    Am unable to get it, why the program is failing in case of global
    variables.


    Regards
    Sunil
  • Michael Mair

    #2
    Re: Regarding global variables memory allocation and gtime funtion

    sunil wrote:
    Hi,
    >
    Am developing one shared library for one application.
    In that .so am having one global array of structure ( two more
    different structure pointers in this struct).
    Once the application is launched, then I am allocating the memory on
    the heap for the internal structures.
    >
    To print the log messages I am using the below mentioned global
    variables and time header functions.
    >
    variables
    static time_t ltime;
    static char timeString[TIMESTAMP_SIZE];
    static struct tm *newtime;
    >
    functions
    time(&ltime);
    newtime = gmtime(&ltime);
    memset(timeStri ng,'\0',TIMESTA MP_SIZE);
    strftime(timeSt ring, TIMESTAMP_SIZE, "%Y-%m-%d-%X%z------",newtime);
    >
    And am using the above timestring variable to print the timestamp.
    >
    It is working fine on unix boxes.
    It is failing in some boxes after printing some log messages.
    After executing the gmtime function, the values of the internal
    structures are becoming null.
    And the program is trying to access the null values and it is failing.
    >
    >
    If I make the above time related variables to static(earlier the
    variables are global) then the program is executing fine without any
    problems.
    >
    Am unable to get it, why the program is failing in case of global
    variables.
    I do not see any obvious problems.

    Maybe one of the following questions' answer helps you find the
    problem:
    - can you break it down to changing only one variable's linkage
    to internal saving the day?
    - do you need external linkage for ltime, newtime, and timeString?
    - What happens if you change the variable names?
    - do you check the return values of time() (for (time_t)-1),
    gmtime() (for NULL) and strftime (for 0)?
    - are you very sure that you do not at one point change the address
    of the "internal structures" (and this happens to include the static
    storage duration object the address of which is returned by gmtime())?
    - What happens if you use
    struct tm newtime, *pNewTime;
    int IsTimeValid;
    pNewTime = gmtime(&ltime);
    if (pNewTime) {
    IsTimeValid = 1;
    newtime = *pNewTime;
    } else {
    IsTimeValid = 0;
    }
    ....
    if (IsTimeValid) {
    memset(....);
    if (0 == strftime(....))
    ....

    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.

    Comment

    Working...