By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,458 Members | 1,215 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,458 IT Pros & Developers. It's quick & easy.

Regarding global variables memory allocation and gtime funtion

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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.