Libpcap + socket programing

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?
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 !
Hah thanks wow I feel like an idiot now ^_^
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,)
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.
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.