C程序随机改变int的值

时间:2016-08-03 20:21:08

标签: c server flush integer-overflow

我真的很困惑。我有一个C服务器,它工作得很好,但后来我添加了一些代码,我认为它工作正常,直到我的程序随机开始将int的值更改为负值。

基本上我让我的服务器输出发送的总字节数和传输中途,总是大约2100000000字节,总字节变为负数。这是我输出文件的一个示例。如果你查看我的代码,这个值就不会变成负数。所以我怀疑它有些怪异。

"345000","1470253912","59203","5592","2069901108"
"348000","1470253912","475539","4194","2092449162"
"351000","1470253912","830291","2796","2112043464"
"354000","1470253913","243217","1398","2133985176"
"357000","1470253913","708686","13980","-2135434834"
"360000","1470253914","173646","9786","-2109094024"
"363000","1470253914","514938","6990","-2089413400" 

无论如何我添加的东西,我评论了它,我仍然遇到了同样的错误。仅供参考,我在代码中注释了标签" NEW STUFF ADDED"。 (因为这篇文章我添加了它:Terminating C program on command line, but make sure writer is finished writing

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <errno.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <arpa/inet.h>
    #include <sys/wait.h>
    #include <signal.h>
    #include <sys/time.h>



int PORT_NUM = 0; 
int RecordRate = 3000; 
FILE *fp;

typedef struct timeval timeval;
timeval time_;


void error(const char *msg)
{
    perror(msg);
    exit(1);
}

 //NEW STUFF ADDED
 //void sig_handler(int signo)
 //{
 //  if (signo == SIGINT) {
 // printf("received SIGINT\n");
 // exit(0);
 // fflush(fp);
 // }
 //}

 int main(int argc, char *argv[])
 {
     int sockfd, newsockfd, portno;
     socklen_t clilen;
     char buffer[1000000];
     struct sockaddr_in serv_addr, cli_addr;
     int n;



     PORT_NUM = atoi(argv[1]);  
     fp = fopen(argv[2],"w");   

//       NEW STUFF ADDED
//       if (signal(SIGINT, sig_handler) == SIG_ERR)
//       printf("\ncan't catch SIGINT\n");

     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     if (sockfd < 0) 
            error("ERROR opening socket");
     bzero((char *) &serv_addr, sizeof(serv_addr));
     //portno = atoi(argv[1]);
     portno = PORT_NUM;
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons(portno);
     if (bind(sockfd, (struct sockaddr *) &serv_addr,
                        sizeof(serv_addr)) < 0) 
                        error("ERROR on binding");
     listen(sockfd,10);
     clilen = sizeof(cli_addr);



     newsockfd = accept(sockfd, 
                             (struct sockaddr *) &cli_addr, 
                             &clilen);
     if (newsockfd < 0) 
                error("ERROR on accept");


    int counter = 0;    
    int total_bytes_sent = 0;

     while(1){  

     bzero(buffer,1000000);
     n = read(newsockfd,buffer,999999);


     if (n < 0) {
     error("ERROR reading from socket");
     }
     else if (n != 0) {





             total_bytes_sent += n;
             gettimeofday(&time_, NULL);

             if(counter%RecordRate==0){
             printf("counter  %d \n", counter); 
             printf("Bytes Sent  %d \n", total_bytes_sent);
             fprintf(fp,"\"%d\",\"%ld\",\"%d\",\"%d\",\"%d\"\n", counter, time_.tv_sec, time_.tv_usec, n,total_bytes_sent);
             }




             counter++; 



     }

     }
     fclose(fp);
     close(newsockfd);
     close(sockfd);
     return 0; 
 }
我发誓,我不是在拖钓。我担心这是我添加的内容,但是一旦我评论出来,我就得到了同样的错误。事情是我添加代码后才出现错误。那么因果关系,相关性,没有联系?

这怎么可能发生?为什么总是大约2100000000字节。

这不是世界末日。我的意思是我可以硬编码确保价值总是积极的东西,但我很好奇这是如何发生的。谢谢。

1 个答案:

答案 0 :(得分:6)

仅仅因为普通的带符号int变量在2 31 -1(2147483647)处溢出并变为负数。

由于这个数量级不足以跟踪您需要的值,因此您应该使用64位变量 - 将变量声明为long long - 并在地方适当注意此更改您正在将此值输出到文本流,您应该很好。