分割结构数组由指针组成的分段错误

时间:2012-03-23 18:48:19

标签: c++ c dynamic-memory-allocation

  

可能重复:
  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;

我使用malloc和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;


                         }

               }
}

3 个答案:

答案 0 :(得分:2)

复制元素时,例如,malloc sizeof(the_array[i].name)) strlen(the_array[i].name)+1,这是字符指针的大小。如果名称超过3个字节,则在复制时会覆盖未分配的内存。您需要分配qsort(list, N, sizeof(DATA), DataTimestampCompare);。同样适用于其他要素。即使这样,您也会遇到以下问题:节点X中的名称可能比您要复制到其中的节点Y中的名称短。整个战略注定要失败。

是否有某些原因你只是不交换节点?或者甚至更好 {{1}}

答案 1 :(得分:1)

交换乱序数组元素的代码有几个问题。您的问题被标记为CC++,但在C++中您可以简单地说:

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

您只需要交换它们包含的结构或指针。您现有的代码没有为(不必要的)字符串复制分配足够的内存,并且内存泄漏可怕。

答案 2 :(得分:0)

如果没有看到你如何进行分配并使用数组,那么很难回答,但很可能你只是为结构分配内存而忘记了成员指针。您也必须为它们分配内存,否则如果您尝试访问其中一些内存,则会出现seg错误。

*the_array->name例如,如果只为“the_array”分配内存,则会导致seg错误。我猜你的排序算法试图访问一些struct的属性。