One problem encountered in mind

Use libpcap development in the use of Linux environment, there have been mistakes in the make when debugging with gdb found the problem in the following sections

int
main(int argc,char * argv[]){
	.....
	pcap_if_t ** alldevsp;
	pcap_findalldevs(alldevsp,err_buf);
	.....
}
结果:出现  segment fault

Then start looking for problems, found himself understanding of the C language is not impressive enough
first

pcap_if_t ** alldevsp;

This statement defines a two alldevsp pointer, a pointer to the variable pcap_if_t * type, i.e., four bytes of data in the address space of the address alldevsp this variable is stored in a variable pcap_if_t *

Then observe the function call

pcap_findalldevs(alldevsp,err_buf);

This document prototype is a function of the number of

int pcap_findalldevs(pcap_if_t **alldevsp, char *errbuf);

Pcap_if_t ** need to pass a parameter to the type of data alldevsp
but their experience point of view, the function of this parameter is made obvious thing
official documentation says this function returns the list of all network cards describe the structure of the parameter alldevsp
speculation about the process is like this

pcap_if_t * temp = NULL;
temp = getListOfDevs(/);//通过此方法取得链表并返回链表第一个元素的指针
//temp现在指向这个链表的头
*alldevsp = temp;

If before practice

pcap_if_t ** alldevsp;
pcap_findalldevs(alldevsp,err_buf);

(Gcc compiler will no variable assigned an initial value 0 as an initial value of life)
it is not difficult to see
the value of the current parameter is NULL alldevsp
i.e. two variable alldevsp the address pointer points to (initialized as NULL)
then the function of this address was NULL * (address assignment) operation, is certainly wrong ah

You simply can not write so
is the proper wording

pcap_if_t * alldevsp;
pcap_findalldevs(&alldevsp,err_buf);

This means that the variable is passed to a function address alldevsp, and then after the function * operation, assignment to this address, the assigned value is the address of the list was the head of
that is alldevsp after calling the method, points to the head of the list
so that you step may be performed subsequent normal

. . . .
Yes, very simple question, but I had that thought up, indicating that learning to pay attention to detail ah, a little knowledge can not

Released seven original articles · won praise 1 · views 706

Guess you like

Origin blog.csdn.net/qq_41957544/article/details/89782657