pointer be changed strangely

E

ellre923

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?
 
A

Alan Johnson

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?

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
 
B

BigBrian

Alan said:
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.

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.
 
H

Howard

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?

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
 
E

ellre923

Yes!"He's seeing his "howlong" value change BEFORE calling
::select(). " What cause this is what I want to know.
 
E

ellre923

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;}
 
E

ellre923

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.
 
P

Prawit Chaivong

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;}

Is this program multithread?

Regards,
 
P

Prawit Chaivong

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;}

I apologize if this post is duplicated.

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

Regards,
 
H

Howard

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.

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

-Howard
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top