Mögliche Duplizieren:
How do I find a segfault in my C++ program?Segmentation fault während Struktur Sortierung Array aus Zeiger
Ich bin der Segmentierungsfehler bekommen, während die Struktur Sortierung Hier ist meine Struktur
typedef struct
{
char *id;
char *timestamp;
char *name;
char *text;
}DATA;
DATA *the_array = NULL;
Ich bin dynamisch den Speicher mit mallo zuweisen c und realloc. Jetzt verwende ich bubblesort, um diese Struktur zu sortieren. Ich verwende Blutvergießen C/C++ IDE unter Windows 7. Hinzufügen des Codes, wo ich die Ausnahme bekomme.
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);
}
}
}
}
Kann ich es tun wie dieses
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;
}
}
}
Wenn Sie ein minimales Beispiel des Codes, der das Problem demonstriert, veröffentlichen, können einige von denen, die nicht psychisch sind, möglicherweise helfen. – AShelly
fügen Sie Code hinzu, und fügen Sie es bitte mit Code-Tag hinzu! wie ein Programm, keine Zeitung! – rene
@AShelly: Ich habe den Code hinzugefügt, wo ich die Ausnahme bekomme, ich bekomme die Ausnahme unter Windows, so kann nicht das Problem erraten – Chetan