Regarding global variables memory allocation and gtime funtion

S

sunil

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(timeString,'\0',TIMESTAMP_SIZE);
strftime(timeString, 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
 
M

Michael Mair

sunil said:
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(timeString,'\0',TIMESTAMP_SIZE);
strftime(timeString, 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top