Re: Regarding global variables memory allocation and gtime funtion



On Feb 28, 12:17 pm, Paul Pluzhnikov <ppluzhnikov-...@xxxxxxxxx>
wrote:
sunil <sunil....@xxxxxxxxx> writes:

...

time(&ltime);
newtime = gmtime(&ltime);
memset(timeString,'\0',TIMESTAMP_SIZE);

The memset above is likely unnecessary ...

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.

Above code is not thread-safe. If you are doing this in a
multi-threaded program, then you deserve exactly what you got. Use
gmtime_r() instead.

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

If your program is single-threaded, then you have a buffer overflow
bug elsewhere, and by making the variables static simply changes
the layout of final executable, such that some other variable is
getting stepped on. IOW, the bug is still present, but you have
yet to observe it.

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.

If the variables are static the program is working fine.

gmttime gmttime_r
global N N
static Y Y

Like the above table the application is functioning.
Not sure why the application is working properly if I chnage to
static.

Sunil.
.



Relevant Pages