[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
"Segmentation fault", the result is curious!
Hello, all,
Another hardworking day has passed. At last, the problem focuses on the
location of "return (TCL_OK);" sentence in the function "int
???Agent::command(...)". Here I take Ping program in Marc Greis' nstutorial
packet as an example, I made a test to it , the result is the same as my
program's.
When the command line follow with three parameters, "return (TCL_OK);"
should be written in the correction location(as bellow).
.
.
.
if(argc == 3){
if( strcmp(argv[1], "send") == 0){
for(int i=0; i<atoi(argv[2]); i++){
Packet* pkt = allocpkt();
hdr_ping* hdr = (hdr_ping*)pkt->access(off_ping_);
hdr->ret = 0;
hdr->send_time = Scheduler::instance().clock();
send(pkt, 0);
}
return (TCL_OK); /* the correct position*/
}
}
.
.
.
If you move the sentence down to the next brace( as below), when
"send(pkt, 0);" is executed, the program will quit and return the
information "segmentation fault(core dumped)".
.
.
.
if(argc == 3){
if( strcmp(argv[1], "send") == 0){
for(int i=0; i<atoi(argv[2]); i++){
Packet* pkt = allocpkt();
hdr_ping* hdr = (hdr_ping*)pkt->access(off_ping_);
hdr->ret = 0;
hdr->send_time = Scheduler::instance().clock();
send(pkt, 0);
}
}
return (TCL_OK); /* the incorrect position*/
}
.
.
.
The corresponding command line in tcl file is "$ns at 0.5 "$p0 send 3".
When there are two parameters in command line, the problem doesn't occur. It
is curious to me. My system is Linux Slackware 3.4, and NS packet is
ns-allinone-2.1b4.
If somebody know the answer to it, I would like to know too and would be
grateful.
Best thanks to Ng Kwang Loong Stanley, Wee Ching Wi and Seo, Chi-Duk who
gave me useful advises.
ZXG
Ping.cc
Ping.tcl
ping.h