Connecting Tech Pros Worldwide Forums | Help | Site Map

Problem with time() using g++

Mike Alexeev
Guest
 
Posts: n/a
#1: Jul 22 '05
Hi all,
I have come across very strange behavior of the return value of the
time() function. Below is the program to illustrate the problem: The
value of NOW somehow changes after the call to the ifstream
constructor.

#include <iostream>
#include <fstream>
#include <time.h>

int main(int argc, char** argv)
{
if (argc != 2)
{
std::cout << "Usage: ctest filename" << std::endl;
exit(-1);
}
time_t NOW = time(0);
std::cout << "Start time before the call to ifstream(): NOW="
<< NOW << std::endl;
std::ifstream infile(argv[1]);
std::cout << "Start time after the call to ifstream(): NOW="
<< NOW << std::endl;
infile.close();
exit(0);
}

I tried this program with g++ 3.1.1 compiler on Solaris 5.8 and here
is the output:
[color=blue]
> g++ -o test test.cxx
> test test.cxx[/color]
Time before the call to ifstream(): NOW=1083075439
Time after the call to ifstream(): NOW=27503[color=blue]
>[/color]

Given the signature of the time() (time_t time(time_t* t)) I was not
able to find any reasonable explanation how the call to the ifstream
constructor could affect the local variable it does not know about so
any help would be greatly appreciated.

By the way, I tried the same program with CC Sun Workshop 6.2 and it
works fine.[color=blue]
> CC -o test test.cxx
> test test.cxx[/color]
Time before the call to ifstream(): NOW=1083075848
Time after the call to ifstream(): NOW=1083075848[color=blue]
>[/color]
---
Thanks,
Mike Alexeev
Gary Labowitz
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Problem with time() using g++


"Mike Alexeev" <michael.alexeev@qwest.com> wrote in message
news:85062ee4.0404270631.63ef8eff@posting.google.c om...[color=blue]
> Hi all,
> I have come across very strange behavior of the return value of the
> time() function. Below is the program to illustrate the problem: The
> value of NOW somehow changes after the call to the ifstream
> constructor.
>
> #include <iostream>
> #include <fstream>
> #include <time.h>
>
> int main(int argc, char** argv)
> {
> if (argc != 2)
> {
> std::cout << "Usage: ctest filename" << std::endl;
> exit(-1);
> }
> time_t NOW = time(0);
> std::cout << "Start time before the call to ifstream(): NOW="
> << NOW << std::endl;
> std::ifstream infile(argv[1]);
> std::cout << "Start time after the call to ifstream(): NOW="
> << NOW << std::endl;
> infile.close();
> exit(0);
> }
>
> I tried this program with g++ 3.1.1 compiler on Solaris 5.8 and here
> is the output:
>[color=green]
> > g++ -o test test.cxx
> > test test.cxx[/color]
> Time before the call to ifstream(): NOW=1083075439
> Time after the call to ifstream(): NOW=27503[color=green]
> >[/color]
>
> Given the signature of the time() (time_t time(time_t* t)) I was not
> able to find any reasonable explanation how the call to the ifstream
> constructor could affect the local variable it does not know about[/color]
so[color=blue]
> any help would be greatly appreciated.
>
> By the way, I tried the same program with CC Sun Workshop 6.2 and it
> works fine.[color=green]
> > CC -o test test.cxx
> > test test.cxx[/color]
> Time before the call to ifstream(): NOW=1083075848
> Time after the call to ifstream(): NOW=1083075848[/color]

Works fine with gcc 3.2 on W2K.
Time to reinstall!
--
Gary


Jeff Schwab
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Problem with time() using g++


Mike Alexeev wrote:[color=blue]
> Hi all,
> I have come across very strange behavior of the return value of the
> time() function. Below is the program to illustrate the problem: The
> value of NOW somehow changes after the call to the ifstream
> constructor.
>
> #include <iostream>
> #include <fstream>
> #include <time.h>
>
> int main(int argc, char** argv)
> {
> if (argc != 2)
> {
> std::cout << "Usage: ctest filename" << std::endl;
> exit(-1);
> }
> time_t NOW = time(0);
> std::cout << "Start time before the call to ifstream(): NOW="
> << NOW << std::endl;
> std::ifstream infile(argv[1]);
> std::cout << "Start time after the call to ifstream(): NOW="
> << NOW << std::endl;
> infile.close();
> exit(0);
> }
>
> I tried this program with g++ 3.1.1 compiler on Solaris 5.8 and here
> is the output:
>
>[color=green]
>>g++ -o test test.cxx
>>test test.cxx[/color]
>
> Time before the call to ifstream(): NOW=1083075439
> Time after the call to ifstream(): NOW=27503
>
>
> Given the signature of the time() (time_t time(time_t* t)) I was not
> able to find any reasonable explanation how the call to the ifstream
> constructor could affect the local variable it does not know about so
> any help would be greatly appreciated.
>
> By the way, I tried the same program with CC Sun Workshop 6.2 and it
> works fine.
>[color=green]
>>CC -o test test.cxx
>> test test.cxx[/color]
>
> Time before the call to ifstream(): NOW=1083075848
> Time after the call to ifstream(): NOW=1083075848[/color]

Try putting this after your #include's:

#undef NOW

Alternatively, don't use all-caps identifiers for anything but macros.
Mike Alexeev
Guest
 
Posts: n/a
#4: Jul 22 '05

re: Problem with time() using g++


Jeff Schwab <jeffplus@comcast.net> wrote in message news:<g9mdnTuZ69CwdxPd4p2dnA@comcast.com>...[color=blue]
> Mike Alexeev wrote:[color=green]
> > Hi all,
> > I have come across very strange behavior of the return value of the
> > time() function. Below is the program to illustrate the problem: The
> > value of NOW somehow changes after the call to the ifstream
> > constructor.
> >
> > #include <iostream>
> > #include <fstream>
> > #include <time.h>
> >
> > int main(int argc, char** argv)
> > {
> > if (argc != 2)
> > {
> > std::cout << "Usage: ctest filename" << std::endl;
> > exit(-1);
> > }
> > time_t NOW = time(0);
> > std::cout << "Start time before the call to ifstream(): NOW="
> > << NOW << std::endl;
> > std::ifstream infile(argv[1]);
> > std::cout << "Start time after the call to ifstream(): NOW="
> > << NOW << std::endl;
> > infile.close();
> > exit(0);
> > }
> >
> > I tried this program with g++ 3.1.1 compiler on Solaris 5.8 and here
> > is the output:
> >
> >[color=darkred]
> >>g++ -o test test.cxx
> >>test test.cxx[/color]
> >
> > Time before the call to ifstream(): NOW=1083075439
> > Time after the call to ifstream(): NOW=27503
> >
> >
> > Given the signature of the time() (time_t time(time_t* t)) I was not
> > able to find any reasonable explanation how the call to the ifstream
> > constructor could affect the local variable it does not know about so
> > any help would be greatly appreciated.
> >
> > By the way, I tried the same program with CC Sun Workshop 6.2 and it
> > works fine.
> >[color=darkred]
> >>CC -o test test.cxx
> >> test test.cxx[/color]
> >
> > Time before the call to ifstream(): NOW=1083075848
> > Time after the call to ifstream(): NOW=1083075848[/color]
>
> Try putting this after your #include's:
>
> #undef NOW
>
> Alternatively, don't use all-caps identifiers for anything but macros.[/color]

The name does not matter. I tried it with different ones with the same result.
Closed Thread