assignment makes integer from pointer without a cast
i excute this program and display this error
"assignment makes integer from pointer without a cast" anybody solve this problem thanks in advanced Code:
#include <stdio.h> |
Quote:
your code is a bit hard to read, you could use the CODE tags to preserve formating. It would also help to specify the lines that cause the problem. I guess it's these: Code:
59: node[i].servno=NULL; node[i].servno=(int)NULL; but since NULL is something like (void*)0, that would be quite silly. Just use node[i].servno = 0; or, if you don't like digits, define a symbolik constant. #define ZERO 0 or something like that :) |
thanks all
program generate new error "segmentation error" so what can i do? |
Quote:
so for your program which i compiled as 'test': Code:
enter the nodeid and cluster id to move sid = node[nid1].Cluster_id - node is an array of size NUM_NODES = 16 - nid1 you use as index is 4195824 => hence the segmentation fault - looking at the code I can see you declare nid1 at the begining of main, but assign no value to it => You use an uninitialized value for nid1, which of course leads to a segfault. another tool you might want to use to debug memory allocation etc is valgrind which reports Code:
Use of uninitialised value of size 8 |
i take nid value but this error occured
Program received signal SIGSEGV, Segmentation fault. 0x0076fbf9 in vfprintf () from /lib/libc.so.6 |
ok you should really read some tutorials on how to use gdb and valgrind. There's a lot of stuff on the internet, just google for it.
I'll give you one more example: Code:
$ gdb ./test it segfaults somewhere in libc 2) I use bt (backtrace) to backtrace the calls it shows that vfprintf was called by fprintf which in turn was called in my main(), line 359: fprintf(gp3,"%d\n%d",nid3,did); 3) I use frame 2 to get into the frame of the call #2 from the backtrace 4) I check the arguments of the fprintf function: p gp3 to print the value of gp3 it says $1 = (FILE *) 0x0, which means the file pointer is NULL => the file was not opened successfully. 5) I check the code to see the call to fopen on line 358: gp3=fopen("move","D"); I mean, seriously -- what mode is "D" anyway? If you want a stable program that does not segfault, you should definitely chack result of any operation that may possibly fail. It's a good practice to do so it could look like this: Code:
gp3 = fopen("move", "w"); /* or whatever mode do you want to use */ regards, millgates PS: I also think this thread should be in the Programming section since it is not Red Hat related. |
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
|
hiiii
i compile my NS program in ns2-3.34 and some error generated error is :- str2addr:Address 172674583 outside range of address field length 1024 # ====================================================================== # Define options # ====================================================================== set val(chan) Channel/WirelessChannel; #channel type set val(prop) Propagation/TwoRayGround; #radio-propagation model set val(netif) Phy/WirelessPhy; #network interface type set val(mac) Mac/802_11; #MAC type set val(ifq) Queue/DropTail/PriQueue; #interface queue type set val(ifqlen) 50; #max nbr of packets in ifq set val(ll) LL; #link layer type set val(ant) Antenna/OmniAntenna; #antenna type set val(adhocRP) AODV; #routing protocol set val(x) 500; #x dimension of the topography set val(y) 500; #y dimension of the topography set val(stop) 20.0; #simulation time set opt(gw_discovery) hybrid set val(start-src) 0.7 set val(stop-src) 40 set nbrOfWirelessNodes 16 set nbrOfGateways 32 # ====================================================================== #--------------------------- #--------------------------- #create a simulator object set ns [new Simulator] $ns color 0 Brown #---------------------------------------- #Define The Hierachial Topology Structure #---------------------------------------- $ns node-config -addressType hierarchical #Nbr of domains AddrParams set domain_num_ 4 #Nbr of clusters (=subdomains) in each domain lappend clusterNbr 1 1 1 1 AddrParams set cluster_num_ $clusterNbr #Nbr of nodes in each cluster lappend eilastlevel 4 4 4 4 AddrParams set nodes_num_ $eilastlevel #create trace objects for ns and nam $ns use-newtrace set nstrace [open out.tr w] $ns trace-all $nstrace set namtrace [open out.nam w] $ns namtrace-all-wireless $namtrace $val(x) $val(y) #create a topology object and define topology (500mx500m) set topo [new Topography] $topo load_flatgrid $val(x) $val(y) #Choose method for gateway discovery #NOTE! You must first uncomment GATEWAY_DISCOVERY (i.e. define it) in aodv.h! if {$opt(gw_discovery) == "proactive"} { Agent/AODV set gw_discovery 0 } if {$opt(gw_discovery) == "hybrid"} { Agent/AODV set gw_discovery 1 } #create God (General Operations Director) create-god [expr $nbrOfWirelessNodes+$nbrOfGateways] #-------------------------------------- #Configure For Gateway and Mobile Nodes #-------------------------------------- #Use hierarchical addresses for GWs and MNs set chan1 [new $val(chan)] #configure for gateway $ns node-config -adhocRouting $val(adhocRP) \ -llType $val(ll) \ -macType $val(mac) \ -ifqType $val(ifq) \ -ifqLen $val(ifqlen) \ -antType $val(ant) \ -propType $val(prop) \ -phyType $val(netif) \ -topoInstance $topo \ -channel $chan1 \ -agentTrace ON \ -routerTrace ON \ -macTrace ON \ -movementTrace OFF #create gateway and set initial coordinates $ns node-config -wiredRouting ON set gw(0) [$ns node 1.0.0] $gw(0) random-motion 0 $gw(0) set X_ 50.0 $gw(0) set Y_ 70.0 $gw(0) set Z_ 0.0 $ns node-config -wiredRouting ON set gw(1) [$ns node 2.0.2] $gw(1) random-motion 0 $gw(1) set X_ 350.0 $gw(1) set Y_ 70.0 $gw(1) set Z_ 0.0 $ns node-config -wiredRouting ON set gw(2) [$ns node 3.0.0] $gw(2) random-motion 0 $gw(2) set X_ 100.0 $gw(2) set Y_ 350.0 $gw(2) set Z_ 0.0 $ns node-config -wiredRouting ON set gw(3) [$ns node 4.0.0] $gw(3) random-motion 0 $gw(3) set X_ 350.0 $gw(3) set Y_ 350.0 $gw(3) set Z_ 0.0 #create mobile nodes in the same domain as gw(0) $ns node-config -wiredRouting OFF set temp {1.0.1 1.0.2 1.0.3 1.0.4} for {set i 4} {$i < 8} {incr i} { set mobile($i) [$ns node [lindex $temp [expr $i-4]]] $mobile($i) base-station [AddrParams addr2id [$gw(0) node-addr]] } #create mobile nodes in the same domain as gw(1) $ns node-config -wiredRouting OFF set temp {2.0.1 2.0.2 2.0.3 2.0.4} for {set i 8} {$i < 15} {incr i} { set mobile($i) [$ns node [lindex $temp [expr $i-8]]] $mobile($i) base-station [AddrParams addr2id [$gw(1) node-addr]] } #create mobile nodes in the same domain as gw(2) $ns node-config -wiredRouting OFF set temp {1 3.0.2 3.0.3 3.0.4} for {set i 12} {$i < 16} {incr i} { set mobile($i) [$ns node [lindex $temp [expr $i-12]]] $mobile($i) base-station [AddrParams addr2id [$gw(2) node-addr]] } #create mobile nodes in the same domain as gw(3) $ns node-config -wiredRouting OFF set temp {4.0.1 4.0.2 4.0.3 4.0.4} for {set i 16} {$i < 20} {incr i} { set mobile($i) [$ns node [lindex $temp [expr $i-16]]] $mobile($i) base-station [AddrParams addr2id [$gw(3) node-addr]] } #toplogy file source topo.tcl puts "" puts "gw0 = [$gw(0) node-addr] = [AddrParams addr2id [$gw(0) node-addr]]" puts "gw1 = [$gw(1) node-addr] = [AddrParams addr2id [$gw(1) node-addr]]" puts "gw2 = [$gw(2) node-addr] = [AddrParams addr2id [$gw(2) node-addr]]" puts "gw3 = [$gw(3) node-addr] = [AddrParams addr2id [$gw(3) node-addr]]" for {set i 4} {incr i} { puts "mobile($i) = [$mobile($i) node-addr] = [AddrParams addr2id [$mobile($i) node-addr]]" } puts "" #Mark the lookup servers as red $gw(0) color red $gw(1) color red $gw(2) color red $gw(3) color red #Set the labels for the lookup servers $ns at 0.0 "$gw(0) label LS1" $ns at 0.0 "$gw(1) label LS2" $ns at 0.0 "$gw(2) label LS3" $ns at 0.0 "$gw(3) label LS4" #Set the labels for the mobile nodes for {set i 4} {$i < 20} {incr i} { $ns at 0.0 "$mobile($i) label MN[expr $i-4]" } #Mark the provider nodes as green for {set i 4} {$i < 20} {incr i 4} { $ns at 0.1 "$mobile($i) add-mark m1 green circle" } # Get the source & destination nodes from the file set f1 [open "result.tr" "r"] set f4 [open "move" "r"] set snode [gets $f1 source] set dnode [gets $f1 dest] set scnode [gets $f1 sclust] set dcnode [gets $f1 dclust] set mnode [gets $f4 movenode] set did [gets $f4 mclust] puts "source:$source" puts "destination:$dest" puts "sclust $sclust" puts "dclust:$dclust" puts "node to move:$movenode" switch $sclust { 1 { set sms 0 } 2 { set sms 1 } 3 { set sms 2 } 4 { set sms 3 } } switch $dclust { 1 { set dms 0 } 2 { set dms 1 } 3 { set dms 2 } 4 { set dms 3 } } switch $mclust { 1 { set x 60 set y 80 } 2 { set x 360 set y 80 } 3 { set x 110 set y 360 } 4 { set x 360 set y 360 } } puts "sms:$sms" puts "dms:$dms" puts "x:$x y:$y" #--------------------- #Setup Traffic #--------------------- set src0 [new Agent/UDP] set dst0 [new Agent/Null] $ns attach-agent $mobile([expr $source+4]) $src0 $ns attach-agent $gw($sms) $dst0 $src0 set fid_ 0 $ns connect $src0 $dst0 set cbr0 [new Application/Traffic/CBR] $cbr0 attach-agent $src0 $cbr0 set packetSize_ 512 $cbr0 set interval_ 0.1 $ns at $val(start-src) "$cbr0 start" $ns at $val(stop-src) "$cbr0 stop" set src1 [new Agent/UDP] set dst1 [new Agent/Null] $ns attach-agent $gw($sms) $src1 $ns attach-agent $gw($dms) $dst1 $src1 set fid_ 0 $ns connect $src1 $dst1 set cbr1 [new Application/CBR] $cbr1 attach-agent $src1 $cbr1 set packetSize_ 512 $cbr1 set interval_ 0.1 $ns at $val(start-src) "$cbr1 start" $ns at $val(stop-src) "$cbr1 stop" set src2 [new Agent/UDP] set dst2 [new Agent/Null] $ns attach-agent $gw($dms) $src2 $ns attach-agent $mobile([expr $dest+4]) $dst2 $src2 set fid_ 0 $ns connect $src2 $dst2 set cbr2 [new Application/Traffic/CBR] $cbr2 attach-agent $src2 $cbr2 set packetSize_ 512 $cbr2 set interval_ 0.1 $ns at $val(start-src) "$cbr2 start" $ns at $val(stop-src) "$cbr2 stop" $ns at $val(start-src) "$ns trace-annotate \"MN $source requesting service from MN $dest\"" #--------------------- #Setup Node Movement #--------------------- $ns at 0.1 "$mobile([expr $movenode+4]) setdest $x $y 250" $ns at 0.1 "$ns trace-annotate \"MN $movenode starts moving at t=0.1 s.\"" #----------------------------------- #Define Node Initial Position In Nam #----------------------------------- #20 defines the node size in nam, must adjust it according to your scenario #The function must be called after mobility model is defined for {set i 4} {$i < 20} {incr i} { $ns initial_node_pos $mobile($i) 20 } #----------------------------------- #Tell Nodes When The Simulation Ends #----------------------------------- for {set i 4} {$i < 20} {incr i} { $ns at $val(stop).0 "$mobile($i) reset"; } $ns at $val(stop).0 "$gw(0) reset"; $ns at $val(stop).0 "$gw(1) reset"; $ns at $val(stop).0 "$gw(2) reset"; $ns at $val(stop).0 "$gw(3) reset"; $ns at $val(stop).0001 "stop" $ns at $val(stop).0002 "puts \"NS EXITING...\" ; $ns halt" proc stop {} { global ns nstrace namtrace $ns flush-trace close $nstrace close $namtrace #exec nam out.nam & exit 0 } puts "Starting simulation..." $ns at 0.0 "$ns set-animation-rate 5ms" $ns run |
All times are GMT -5. The time now is 07:06 PM. |