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

  • Home
  • SEARCH
  • 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 9234141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:43:11+00:00 2026-06-18T06:43:11+00:00

Situation I was trying to implement a more interesting mergesort that creates a random

  • 0

Situation

I was trying to implement a more interesting mergesort that creates a random length array with random values and then randomizes them, but after debugging and compiling it segfaults. I don’t know why it segfaults, but I’m sure it’s related to memory allocation.

Question

Why does this code cause a segfault?

Code

#include <stdio.h>
#include <stdlib.h>
#include <time.h>


// Declare some stuff up front

int array_size(int *array);
int print_array(int *array);

//Some decade old main function coming at you

int main() {

    //Concerned with the integrity of my rand
    srand( (unsigned)time( NULL ));

    //A global, random length array between 1 and 100?
    int *array;
    array = malloc(sizeof(*array) * ((rand() % 100) + 1));


    init_array(*array);




    getchar();
    return 0;
}

int init_array(int *array)  {
    //Base case
    array[0] = 1;
    //random values for i in array
    int i;
    for(i = 1; i <= array_size(array); i++)  {
          array[i] = rand() % array_size(array) + 1;
    }
    //randomize the random values in the random length array
    for (i = 0; i < (array_size(array) - 1); i++)
    {
        unsigned int swapA = (rand() % array_size(array)) + 1;
        int a = array[swapA];
        array[swapA] = array[i];
        array[i] = a;
    }
    //output random array, then mergeSort the array
    print_array(array);
    sort_array(array);
    return 0;
}

//Get my array.Length
int array_size(int *array) {
    return sizeof(array)/sizeof(array[0]);
}

//Output array
int print_array(int *array) {
     int i;
     for(i = 0; i < (array_size(array) + 1); i++) {
           printf("%d\n", array[i]);
     }
     return 0;
}
     //merge the array after sorting
void merge_array(int *array, int low, int split, int high)  {
     int sorted[high-low+1];
     int a = 0;
     int b = low;
     int c = split + 1;
     //iterate from beginning to middle and from middle to end in parallel
     while(b <= split && c <= high)
     {
            if(array[b] < array[c])
            {
                sorted[a++] = array[b++];
            }
            else
            {
                sorted[a++] = array[c++];
            }
     }

     while(b <= split) sorted[a++] = array[b++];
     while(c <= high)  sorted[a++] = array[c++];
     int i;
     for(i = 0; i < a; i++) {
           array[i+low] = sorted[i];
     }
     print_array(array);            //Print sorted array
}
     //Sort the array
int sort_array(int *array, int low, int high) {
    int split = ( low + high ) / 2;
    if( low < high ) {
        sort_array(array, low, split);
        sort_array(array, split + 1, high);
        merge_array(array, low, split, high);
    }

}
  • 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-06-18T06:43:13+00:00Added an answer on June 18, 2026 at 6:43 am
    return sizeof(array)/sizeof(array[0]);
    

    The above statement evaluates to 1 (assuming sizeof(int *) = sizeof(int), as pointed out by H2CO3).

    Try something like this,

    int main() {
    
    //Concerned with the integrity of my rand
    srand( (unsigned)time( NULL ));
    
    //A global, random length array between 1 and 100?
    int *array;
    int number_of_elements = (rand() % 100) + 1;
    array = malloc(sizeof(*array) * num_of_elements);
    
    init_array(*array, num_of_elements);
    
    getchar();
    return 0;
    

    }

    Pass the number of elements as arguments to init_array instead of calculating it every time.

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

Sidebar

Related Questions

I am trying to find a solution that will resolve a recurring deadlock situation
I'm trying to implement a model for private messages between two or more users.
i'm having a weird situation. i'm trying to implement a 10+ years old pci
I'm trying to implement a program that has a producer and N (N >=
More specifically, I'm trying to simulate the Pokemon TCG, if that helps. Currently, I
I'm trying to implement a chart that can handle real time data, which comes
So I'm essentially trying to implement an AIR Native Extension that does the physics
I am trying to implement some sort of MVC in Java. Actually it's more
Situation: I'm trying run an https store (xcart) under one domain secure.example.com and I
I am trying to create a situation where one or none of a grouping

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.