cplusplus.com
C++ : Forum : UNIX/Linux Programming : Libpcap + socket programing
 
cplusplus.com
Information
Documentation
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs


solved Libpcap + socket programing

MaxT (26)
Ok So I want to be be able to grab the IP in human readable form from a device. (I know there are many ways of doing this)
I've come across something odd though...

after this section of code my program terminates prematurely...
1
2
3
4
5
6
7
    char * buff;
    printf("Device: %s\n", dev);
    inet_ntop(AF_INET,&maskp,buff,INET_ADDRSTRLEN);
    printf("Mask : %s \n", buff);
    inet_ntop(AF_INET,&netp,buff,INET_ADDRSTRLEN);
    printf("Ip: %s\n",buff);
    

As in it carries out the final printf before quitting .... the next line after that is
printf("gotpastsetup");
and that is not run....(after that there is a whole bunch of other stuff)
I'm extremely confused why is it exiting?
kevinchkin (448)
I guess it's because you are not allocating any memory for char *buf
And since no memory has been allocated it gives segmentation fault when you try to print it.

Try this:

 
char buf[100]; 


OR

 
char *buf  = new char [100]; 


Hope this helps !
MaxT (26)
Hah thanks wow I feel like an idiot now ^_^
MaxT (26)
Ok Just one more question.

why would the program run fine inside an IDE like codeblocks but once I compile it and run it from the terminal it immediately segfaults o_0

(I'm a bit new to programming under a unix environment,)
PanGalactic (1558)
That's usually due to bugs inside your code that are dependent on memory layout. This is very typical when an invalid or uninitialized pointer is dereferenced.
MaxT (26)
Found it, I was passing args to main and using them without checking to see if any args had been passed or not.
Topic archived. No new replies allowed.