Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7690155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:19:51+00:00 2026-05-31T20:19:51+00:00

Possible Duplicate: How do I find a segfault in my C++ program? I am

  • 0

Possible Duplicate:
How do I find a segfault in my C++ program?

I am getting the segmentation fault while sorting the structure
Here is my structure

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

DATA *the_array = NULL;

I am dynamically allocating the memory using malloc and realloc.
Now I am using bubblesort to sort this structure.
I am using bloodshed c/c++ ide under Windows 7.
Adding the code where I am getting the exception.

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);



                             }
                             }
                   }
    }

Can I do it like this

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;


                         }

               }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T20:19:52+00:00Added an answer on May 31, 2026 at 8:19 pm

    When copying elements, you are, for example, mallocing sizeof(the_array[i].name)), which is the size of a character pointer. If the name is longer than 3 bytes, then you are overwriting unallocated memory when you copy into it. You need to allocate strlen(the_array[i].name)+1. Similarly for other elements. And even then you have the issue that the name in node X may be shorter than the name in node Y which you are copying into it. This whole strategy is doomed to failure.

    Is there some reason you are just not swapping the nodes? Or even better doing
    qsort(list, N, sizeof(DATA), DataTimestampCompare);

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Python program to find fibonacci series. More Pythonic way. Hey, i was
Possible Duplicate: Find and Replace more than 1 word? I want to use jQuery
Possible Duplicate: Easiest way to find duplicate values in a JavaScript array Javascript array
Possible Duplicate: How to find out where a function is defined? get filename of
Possible Duplicate: On iPhone: Find out what song is currently playing? (in the iPod
Possible Duplicate: How to find a duplicate of length 5 in a single array.
Possible Duplicate: How to find out where a function is defined? I am working
Possible Duplicate: How do you find all subclasses of a given class in Java?
Possible Duplicate: How do I write a regex to find and replace मेरा नाम
Possible Duplicate: Returning reference to a local variable I happened to find this code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.