Problem with time() using g++

M

Mike Alexeev

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:
g++ -o test test.cxx
test test.cxx
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.
CC -o test test.cxx
test test.cxx
Time before the call to ifstream(): NOW=1083075848
Time after the call to ifstream(): NOW=1083075848
 
G

Gary Labowitz

Mike Alexeev said:
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:
g++ -o test test.cxx
test test.cxx
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.
CC -o test test.cxx
test test.cxx
Time before the call to ifstream(): NOW=1083075848
Time after the call to ifstream(): NOW=1083075848

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

Jeff Schwab

Mike said:
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:

g++ -o test test.cxx
test test.cxx

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.
CC -o test test.cxx
test test.cxx

Time before the call to ifstream(): NOW=1083075848
Time after the call to ifstream(): NOW=1083075848

Try putting this after your #include's:

#undef NOW

Alternatively, don't use all-caps identifiers for anything but macros.
 
M

Mike Alexeev

Jeff Schwab said:
Mike said:
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:

g++ -o test test.cxx
test test.cxx

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.
CC -o test test.cxx
test test.cxx

Time before the call to ifstream(): NOW=1083075848
Time after the call to ifstream(): NOW=1083075848

Try putting this after your #include's:

#undef NOW

Alternatively, don't use all-caps identifiers for anything but macros.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top