473,385 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 2562
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Kleenex | last post by:
Reason: I am working on an embedded project which has very limited memory (under 512 bytes, 60 or so of which is stack space), which translates into limited stack space. In order to save on stack...
15
by: MackS | last post by:
The system I am working on supports a subset of C99, among which "standard-compliant VLAs". I've already learnt that VLAs can't have global scope. My question is whether I can safely declare a...
3
by: Pavan | last post by:
Hi i would like to know abt the data variable storage in C like 1.where does global variables gets stored and why is it so .. 2.like wise static ,local variables, as for as i know i remember...
4
by: gamja | last post by:
Hi all. I know that some padding bits are inserted between data members of a structure. Is this rule also applied for the variables on local stack or global??? For example, in following code...
13
by: Jake Barnes | last post by:
I saw this sentence: "The last stage of variable instantiation is to create named properties of the Variable object that correspond with all the local variables declared within the function." ...
37
by: eoindeb | last post by:
Sorry to ask another global variable question, but from reading other posts I'm still not sure whether to use them or not. I have a program with a set function that calls 4 other functions in...
53
by: fdmfdmfdm | last post by:
This is an interview question and I gave out my answer here, could you please check for me? Q. What are the memory allocation for static variable in a function, an automatic variable and global...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
1
by: danep2 | last post by:
Let me start by saying that this is more a question about principle than practice - with the speed of today's computers it's probably rarely an actual issue. Still I'd like to know... If I have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.