2012-03-23 125 views
-1

可能重複:
How do I find a segfault in my C++ program?分段故障

我收到分段錯誤而排序結構 這裏是我的結構

typedef struct 
    { 
    char *id; 
    char *timestamp; 
    char *name; 
    char *text; 
    }DATA; 

DATA *the_array = NULL; 

我使用mallo動態分配內存c和realloc。 現在我正在使用bubblesort對此結構進行排序。 我在Windows 7下使用流血的c/C++ ide。 在我得到異常的地方添加代碼。

for(int i =0;i < num_elements;i++) 
    { 
      if(strcmp("DUP",the_array[i].id)==1) 
      for(int j = i+ i; j < num_elements; j++) 
        { 
         if(strcmp("DUP",the_array[j].id)==1){ 
         float n1 = strtof(the_array[i].timestamp,NULL); 
         float n2 = strtof(the_array[j].timestamp,NULL); 
         // Exchange the elements 
         if(n1 > n2) 
          { 
            // Exchange the id 
            temp_id = (char*)malloc(sizeof(the_array[i].id)); 
            strcpy(temp_id,the_array[i].id); 
            strcpy(the_array[i].id,the_array[j].id); 
            strcpy(the_array[j].id,temp_id); 

            //Exchange the timestamps 
            temp_timestamp = (char*)malloc(sizeof(the_array[i].timestamp)); 
            strcpy(temp_timestamp,the_array[i].timestamp); 
            strcpy(the_array[i].timestamp,the_array[j].timestamp); 
            strcpy(the_array[j].timestamp,temp_timestamp); 

            //Exchange the username 
            temp_username = (char*)malloc(sizeof(the_array[i].name)); 
            strcpy(temp_username,the_array[i].name); 
            strcpy(the_array[i].name,the_array[j].name); 
            strcpy(the_array[j].name,temp_username); 

            //Exchange the text 
            temp_text = (char*)malloc(sizeof(the_array[i].text)); 
            strcpy(temp_text,the_array[i].text); 
            strcpy(the_array[i].text,the_array[j].text); 
            strcpy(the_array[j].text,temp_text); 



          } 
          } 
        } 
    } 

我可以做這樣的

for(int i =0;i < num_elements;i++) 
{ 
     if(strcmp(dup,the_array[i].id)==1) 
     for(int j = i+ i; j < num_elements; j++) 
       { 

        float n1 = strtof(the_array[i].timestamp,NULL); 
        float n2 = strtof(the_array[j].timestamp,NULL); 
        // Exchange the elements 
        if(n1 < n2) 
         { 
          //Change the pointer locations 
          temp_array1 = &the_array[i]; 
          temp_array2 = &the_array[j]; 

          temp_array3=temp_array1; 
          temp_array1=temp_array2; 
          temp_array2=temp_array3; 


         } 

       } 
} 
+4

如果您發佈了一個展示問題的代碼的簡單示例,那麼其中一些不具有通靈能力的人可能會提供幫助。 – AShelly 2012-03-23 18:50:39

+0

添加您的代碼,並且請添加代碼標籤!就像一個不是報紙的節目! – rene 2012-03-23 18:53:26

+0

@AShelly:我已經添加了代碼,我得到了異常,我得到了Windows下的異常,所以無法猜出問題 – Chetan 2012-03-23 19:07:57

回答

2

當複製元素時,例如,您是,它是一個字符指針的大小。如果名稱長於3個字節,則在複製內容時覆蓋未分配的內存。您需要分配strlen(the_array[i].name)+1。對其他元素也是如此。即使如此,您仍然有問題,即節點X中的名稱可能比您複製到其中的節點Y中的名稱短。這整個戰略註定要失敗。

是否有某種原因,您只是不交換節點?或者更好的做 qsort(list, N, sizeof(DATA), DataTimestampCompare);

0

沒有看到你是怎麼做到的分配和使用數組這將是很難回答的,但對於結構最有可能你只是分配內存忘記會員指針。你也必須爲他們分配內存,否則如果你嘗試訪問他們中的一些,你會得到一個seg故障。

*the_array->name例如,如果您僅爲「the_array」分配內存,將導致seg故障。我猜你的排序算法試圖訪問一些結構的屬性。

1

你的代碼交換亂序數組元素有幾個問題。你的問題被標記都CC++C++你可以簡單地說:

// Exchange the elements 
if(n1 > n2) 
{ 
    std::swap(the_array[i], the_array[j]); 
} 

你只需要交換結構或它們所包含的指針。您現有的代碼不會爲(不必要的)字符串複製分配足夠的內存並且存在可怕的內存泄漏。