簡體   English   中英

分段故障和Valgrind調試

[英]Segmentation Fault and Debugging with Valgrind

我在這段代碼中可能遇到了分段問題,使用了Valgrind,但我是新手,無法理解它的含義。有人可以幫助我從以下代碼中刪除錯誤。 Valgrind也有回應。 在代碼之后,給出了valgrind的響應。

send_file1=fopen("Document.txt","rb");
cout<<"what";
if(send_file1==NULL) 
{
if(int nofile = sendto(conn_sock , NoFile , sizeof(NoFile) , 0 ,(struct sockaddr *)&client_addr , sizeof(client_addr))==-1) //Sendin ACK
{
    cout<<"ERROR"<<endl;
    exit(1);
}

}
if(int accept = sendto(conn_sock , accepted , sizeof(accepted) , 0 ,(struct sockaddr *)&client_addr , sizeof(client_addr))==-1) //Sendin ACK
{
    cout<<"ERROR"<<endl;
    exit(1);
}
cout<<"CHECK"<<endl;
fseek (send_file1 , 0 , SEEK_END);
length = ftell (send_file1);
rewind (send_file1);

// allocate memory to contain the whole file:
Buffer = (char*) malloc (sizeof(char)*length);
if (Buffer == NULL) {fputs ("Memory error",stderr); exit (2);}

// copy the file into the buffer:
Read_byt = fread (Buffer,1,length,send_file1);
if (Read_byt != length) {fputs ("Reading error",stderr); exit (3);}



==11113== Invalid read of size 2
==11113==    at 0x41C924D: fseek (fseek.c:40) 
==11113==    by 0x8049B34: main (in /home/mishal/Downloads/Work Moby/Computer     Networks/Project/Phase 1/execute)
==11113==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==11113==  
==11113== 
==11113== Process terminating with default action of signal 11 (SIGSEGV)
==11113==  Access not within mapped region at address 0x0
==11113==    at 0x41C924D: fseek (fseek.c:40)
==11113==    by 0x8049B34: main (in /home/mishal/Downloads/Work Moby/Computer Networks/Project/Phase 1/execute)
==11113==  If you believe this happened as a result of a stack
==11113==  overflow in your program's main thread (unlikely but
==11113==  possible), you can try to increase the size of the
==11113==  main thread stack using the --main-stacksize= flag.
==11113==  The main thread stack size used in this run was 8388608.
==11113== 
==11113== HEAP SUMMARY:
==11113==     in use at exit: 9,067 bytes in 8 blocks
==11113==   total heap usage: 9 allocs, 1 frees, 9,419 bytes allocated
==11113== 
==11113== LEAK SUMMARY:
==11113==    definitely lost: 500 bytes in 5 blocks
==11113==    indirectly lost: 0 bytes in 0 blocks
==11113==      possibly lost: 0 bytes in 0 blocks
==11113==    still reachable: 8,567 bytes in 3 blocks
==11113==         suppressed: 0 bytes in 0 blocks
==11113== Rerun with --leak-check=full to see details of leaked memory
==11113== 
==11113== For counts of detected and suppressed errors, rerun with: -v
==11113== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 19 from 8)

使用-g (調試標志)編譯代碼以從valgrind獲取更多信息,例如源代碼中錯誤的位置。 您正在打開的文件似乎不存在,因此請確保您正在檢查外部調用的返回值(即fopenfseek ,...)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM