2017-02-10 49 views
-3

我正要從一個名爲displaypcapStas()的函數返回無符號長數組:我不能這樣做,我不知道發生了什麼事情: 這是我的c代碼:c代碼的分段錯誤:返回值

unsigned long * displaypcapStats() 
{ 
int   nBytes, testId, num_pcaps; 
char   buffer[1024]; 
socklen_t  addr_size; 
int   index = 0, i, cmd_id, len, parLen, result; 
char   pcapname[256]; 
int statNum1 = 0; 

unsigned long *pcapstats; 
unsigned long *value; 

unsigned long tx_pkts, tx_bytes, rx_pkts, rx_bytes, tx_pkts_op, tx_bytes_op, rx_pkts_op, rx_bytes_op; 
int   east_west, west_east, err_pkts; 
int   passvalue = 0; 

//Create UDP socket clientSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); 

//Configure settings in address struct 
bzero((char *) &serverAddr, sizeof(serverAddr)); 
serverAddr.sin_family = AF_INET; 
serverAddr.sin_port = htons(ATICARA_AUTO_PORT); 
//inet_aton(host, &serverAddr.sin_addr); 
inet_aton(getenv(ATICARA_HOST_IP), &serverAddr.sin_addr); 

//Initialize size variable to be used later on 
addr_size = sizeof(serverAddr); 

buffer[index++] = ((tid>>8)&0xff); 
buffer[index++] = tid & 0xff; 
buffer[index++] = ((DISPLAY_PCAP_STATS >> 8) & 0xff); 
buffer[index++] = DISPLAY_PCAP_STATS & 0xff; 

//Send message to server 
sendto(clientSocket,buffer,index,0,(struct sockaddr *)&serverAddr,addr_size); 

//Receive message from server for num of pcaps 
nBytes = recvfrom(clientSocket,buffer,BUFSIZE,0,(struct sockaddr *)&serverAddr, &addr_size); 
printf("Received Pcap Stats successfull:%d\n", nBytes); 
tid = ((buffer[0] << 8) & 0xff00) | (buffer[1] & 0xff); 
result = ((buffer[2] << 8) & 0xff00) | (buffer[3] & 0xff); 

if (result == 1){ 
    index = 3; 
    num_pcaps = ((buffer[++index] << 8) & 0xff00) | (buffer[++index] & 0xff); 
    printf("\nNumber of pcaps: %d\n", num_pcaps); 

    pcapstats = malloc(8*num_pcaps*sizeof(unsigned long)); 
for(i = 0; i < num_pcaps; i++) { 

     tx_pkts = (((uint64_t)buffer[++index] << 56) & 0xff00000000000000) | 
        (((uint64_t)buffer[++index] << 48) & 0xff000000000000) | 
        (((uint64_t)buffer[++index] << 40) & 0xff0000000000) | 
        (((uint64_t)buffer[++index] << 32) & 0xff00000000) | 
        (((uint64_t)buffer[++index] << 24) & 0xff000000) | 
        (((uint64_t)buffer[++index] << 16) & 0xff0000) | 
        (((uint64_t)buffer[++index] << 8) & 0xff00) | 
        ((uint64_t)buffer[++index] & 0xff); 
     printf("%ld ", tx_pkts); 

     tx_bytes = ... 



     pcapstats[statNum1++] = tx_pkts; 
     pcapstats[statNum1++] = tx_bytes; 
     pcapstats[statNum1++] = rx_pkts; 
     pcapstats[statNum1++] = rx_bytes; 
     pcapstats[statNum1++] = tx_pkts_op; 
     pcapstats[statNum1++] = tx_bytes_op; 
     pcapstats[statNum1++] = rx_pkts_op; 
     pcapstats[statNum1++] = rx_bytes_op; 

    } 
    pcapstats[statNum1++] = '\0'; 
    printf("Pcap stats Display Successfull:%d\n",nBytes); 
    return(pcapstats); 
} 
else { 
    printf("\nPcap stats Display Unsuccessfull!:%d",nBytes); 
    *value = buffer[3]; 
    return value; 
} 
} 

我得到分段錯誤,姓氏。

+6

請張貼小例子;沒有人會讀這個。 –

+0

請發佈[MCVE]。 –

+0

至少添加實際輸出並標記'pythonFunctions.c:1117'這一行。 –

回答

2

您正在修改index兩次without an intervening sequence point

num_pcaps = ((buffer[++index] << 8) & 0xff00) | (buffer[++index] & 0xff); 

(停止尋找時,我發現這有可能是更多的錯誤。)

+0

@ klas Lineback:hi look before num_pcaps =((buffer [++ index] << 8)&0xff00)| (buffer [++ index]&0xff);我提到過索引= 3;這意味着每次在獲取指數值之前都會進行交易,並且會取得下一個值。像4,5,....這不過是緩衝區值。我不是修改索引兩次,而是在增量後取指數值。 –

+0

@AdarshaVerma你錯了。如果((buffer [++ index] << 8)&0xff00)爲零,則執行'(buffer [++ index]&0xff)'。這是'index'的兩個**增量。 –

+0

@CareyGregory'|'不會短路,這段代碼總是有兩個增量表達式被執行(因此是不確定的行爲) –