Skip to main content
deleted 34 characters in body
Source Link
chux
  • 33.7k
  • 2
  • 38
  • 92
  1. Suspect that segmentation fault on large arrays occurs because the list1[] and list2[] ran out of space. With the recursive calls, code is heavily using the stack space. Use malloc() and free() for large arrays instead of VLA[]

  2. Memory allocation could be reduced. Via recursion, this takes > 2n (maybe 4n) memory space. At worst At worst it should be 2n. At a minimum, allocate, populate and sort list1[], then do the same for list2[]2n.

  3. Use size_t rather than int for a integer type that can handle all array indexes.

    // void mergesort (int* list, int len)
    void mergesort (int* list, size_t len)
    
  4. Cope with 0 length.

     // if(len == 1) return;
     if(len <= 1) return;
    
  1. Suspect that segmentation fault on large arrays occurs because the list1[] and list2[] ran out of space. With the recursive calls, code is heavily using the stack space. Use malloc() and free() for large arrays instead of VLA[]

  2. Memory allocation could be reduced. Via recursion, this takes > 2n (maybe 4n) memory space. At worst it should be 2n. At a minimum, allocate, populate and sort list1[], then do the same for list2[].

  3. Use size_t rather than int for a integer type that can handle all array indexes.

    // void mergesort (int* list, int len)
    void mergesort (int* list, size_t len)
    
  4. Cope with 0 length.

     // if(len == 1) return;
     if(len <= 1) return;
    
  1. Suspect that segmentation fault on large arrays occurs because the list1[] and list2[] ran out of space. With the recursive calls, code is heavily using the stack space. Use malloc() and free() for large arrays instead of VLA[]

  2. Memory allocation could be reduced. Via recursion, this takes > 2n (maybe 4n) memory space. At worst it should be 2n.

  3. Use size_t rather than int for a integer type that can handle all array indexes.

    // void mergesort (int* list, int len)
    void mergesort (int* list, size_t len)
    
  4. Cope with 0 length.

     // if(len == 1) return;
     if(len <= 1) return;
    
Source Link
chux
  • 33.7k
  • 2
  • 38
  • 92

  1. Suspect that segmentation fault on large arrays occurs because the list1[] and list2[] ran out of space. With the recursive calls, code is heavily using the stack space. Use malloc() and free() for large arrays instead of VLA[]

  2. Memory allocation could be reduced. Via recursion, this takes > 2n (maybe 4n) memory space. At worst it should be 2n. At a minimum, allocate, populate and sort list1[], then do the same for list2[].

  3. Use size_t rather than int for a integer type that can handle all array indexes.

    // void mergesort (int* list, int len)
    void mergesort (int* list, size_t len)
    
  4. Cope with 0 length.

     // if(len == 1) return;
     if(len <= 1) return;