468,535 Members | 1,586 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,535 developers. It's quick & easy.

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(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
Feb 27 '08 #1
1 2278
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(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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Feb 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Kleenex | last post: by
37 posts views Thread by eoindeb | last post: by
53 posts views Thread by fdmfdmfdm | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.