Problem with time() using g++

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike Alexeev

    Problem with time() using g++

    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

    #2
    Re: Problem with time() using g++

    "Mike Alexeev" <michael.alexee v@qwest.com> wrote in message
    news:85062ee4.0 404270631.63ef8 eff@posting.goo gle.com...[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


    Comment

    • Jeff Schwab

      #3
      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.

      Comment

      • Mike Alexeev

        #4
        Re: Problem with time() using g++

        Jeff Schwab <jeffplus@comca st.net> wrote in message news:<g9mdnTuZ6 9CwdxPd4p2dnA@c omcast.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.

        Comment

        Working...