pointer be changed strangely

Discussion in 'C++' started by ellre923, Jun 6, 2005.

  1. ellre923

    ellre923 Guest

    I use gdb to trace the following code and find "howlong" is changed
    strangely.

    int CmDispatch::waitFor(timeval* howlong) {
    int nfound=-1;
    for(;nfound<0;) {
    CmFdSet& r=onRead_->onSelects();
    CmFdSet& w=onWrite_->onSelects();
    CmFdSet& e=onExcept_->onSelects();
    howlong = queue_->calculateTimeout(howlong);
    //print howlong. howlong=0x8083258
    //then step into select()
    nfound=select(r,w,e,howlong);
    if(nfound<0) {handleError();}}
    return nfound;}

    int CmDispatch::select(CmFdSet& r,CmFdSet& w,CmFdSet& e,timeval*
    howlong) {
    //print howlong. howlong=0x0
    return ::select(nfds_,r,w,e,howlong);}

    There is no code between the change of "howlong". Why this happen?
     
    ellre923, Jun 6, 2005
    #1
    1. Advertisements

  2. ellre923

    Alan Johnson Guest

    In Linux (and some other systems) the timeout value to "select" is
    changed to indicate how much time was left on the timer. The Single
    Unix Specification (and probably POSIX, though I don't have a copy of
    the standard to check) allow this behavior. To be portable, you should
    consider the value of the timeout after a select to be undefined.

    -Alan
     
    Alan Johnson, Jun 6, 2005
    #2
    1. Advertisements

  3. ellre923

    BigBrian Guest

    I don't think this is the issue. He's seeing his "howlong" value
    change BEFORE calling ::select(). Currently, its not apparent to me
    why this would be.
     
    BigBrian, Jun 6, 2005
    #3
  4. ellre923

    Howard Guest

    What's your *real* code look like? You don't show the "print" statement
    you're using to get those values. (Is that some gdb feature?) One thing I
    do see, which may or may not be related, is that you're setting a value for
    howlong in the waitFor function, but that change is only occurring to the
    local copy of that pointer. It will not change the value of the poitner
    passed to waitFor unless you make it a reference (or pointer) to a pointer.
    Perhaps this problem is causing gdb (a tool I know nothing about) to show
    you the wrong "howlong" variable. I know some debuggers can give you
    misleading information when you've got multiple variables with the same
    name. In any case, if you want that howlong parameter to change in the
    calling function (and not just for a local copy), then make it timeval*&
    instead of just timeval*. Likewise if select is going to change it (and you
    want waitFor to see that change).

    -Howard
     
    Howard, Jun 6, 2005
    #4
  5. ellre923

    ellre923 Guest

    Yes!"He's seeing his "howlong" value change BEFORE calling
    ::select(). " What cause this is what I want to know.
     
    ellre923, Jun 7, 2005
    #5
  6. ellre923

    ellre923 Guest

    Here is the code.
    applMain() {
    StringList positional;
    CSTR path=RunEnv::unixPath(*main,&positional);
    if(path==0) exit(1);

    PktCtlClient client(positional);

    CmDispatch& disp=CmDispatch::instance();
    for(;;) {disp.dispatch();}
    exit(0);}

    void CmDispatch::dispatch() {
    dispatch(0);}

    bool CmDispatch::dispatch(timeval* howlong) {
    int nfound;
    static timeval timeout;
    if(anyReady()){howlong=&timeout;}
    nfound=waitFor(howlong);
    notify();
    return nfound>0;}

    int CmDispatch::waitFor(timeval* howlong) {
    int nfound=-1;
    for(;nfound<0;) {
    CmFdSet& r=onRead_->onSelects();
    CmFdSet& w=onWrite_->onSelects();
    CmFdSet& e=onExcept_->onSelects();
    howlong = queue_->calculateTimeout(howlong);
    nfound=select(r,w,e,howlong);
    if(nfound<0) {handleError();}}
    return nfound;}

    int CmDispatch::select(CmFdSet& r,CmFdSet& w,CmFdSet& e,timeval*
    howlong) {
    return ::select(nfds_,r,w,e,howlong);}

    timeval *TimerQueue::calculateTimeout(timeval* howlong) const {
    static timeval timeout;
    if(isEmpty()) return howlong;
    timeval curTime=currentTime();
    timeval ealiest=earliestTime();
    if(ealiest>curTime) {
    timeout=ealiest-curTime;
    if(howlong==0|| *howlong>timeout) {
    howlong=&timeout;}}
    else {
    timeout=TimerQueue::zeroTime();
    howlong=&timeout;}
    return howlong;}
     
    ellre923, Jun 7, 2005
    #6
  7. ellre923

    ellre923 Guest

    Howard, thank you. I use printf() statement instead of gdb print
    function to display the "howlong" value. The value doesn't change. So
    this issue maybe a problem of gdb.
     
    ellre923, Jun 7, 2005
    #7
  8. Is this program multithread?

    Regards,
     
    Prawit Chaivong, Jun 7, 2005
    #8
  9. I apologize if this post is duplicated.

    I just need to know whether your program is multithread app or not?

    Regards,
     
    Prawit Chaivong, Jun 7, 2005
    #9
  10. ellre923

    Howard Guest

    Sounds like it's working correctly, then. Glad I could help.

    -Howard
     
    Howard, Jun 7, 2005
    #10
  11. ellre923

    ellre923 Guest

    It's sigle thread.
     
    ellre923, Jun 8, 2005
    #11
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.