c代码分段错误:返回值

时间:2017-02-10 11:12:35

标签: c sockets malloc swig

我准备从一个名为displaypcapStas()的函数返回unsigned long数组:但是我无法做到这一点,我不知道发生了什么: 这是我的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;
 }
}

我得到了分段错误,姓氏。

1 个答案:

答案 0 :(得分:2)

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

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

(当我发现这个时停下来看。可能会有更多错误。)