c - C 代码段错误 : Return value

标签 c sockets malloc swig

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

我的姓氏出现段错误。

最佳答案

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

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

(当我发现这个时就停止寻找了。可能还有更多错误。)

关于c - C 代码段错误 : Return value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42157983/

相关文章:

c - 比较浮点型和浮点型元素数组时出错

c - 有向图/网络中的随机游走

c - 从文件中读取、计算和写入新文件

c - tcp_cork 和 tcp_nodelay 是否有助于提高 c 中套接字编程的性能

c - 如何使用Telnet 来测试我的socket 程序?

c - malloc 之后的 memset

c - 尝试更改内存地址处的值时出现段错误

c - 如何为字符串动态分配内存空间并从用户那里获取该字符串?

c - 如何将 C 代码转换为汇编的十六进制表示形式?

c# - .NET 相当于 recv?