I've been implementing the mergesort algorithm in C, based on a dynamic array structure. I followed the pseudo-code step by step but I am not getting to the point. Here's how I have defined my structure and how I create and initialize it:
typedef struct dynarray {
void **memory;
size_t allocated;
size_t used;
int index;
} dynarray;
//creates a new, empty, dynarray
void create_dynarray(dynarray **array, size_t size) {
*array = calloc(size, sizeof **array);
(*array)->memory = NULL;
(*array)->allocated = 0;
(*array)->used = 0;
(*array)->index = -1;
}
And here is the implementation of the mergesort:
//function used to slice the dynarray in two subarrays and call merge function
void *dynarray_mergesort(dynarray *param) {
if (dynarray_length(param) > 1) {
size_t size = param->used / sizeof(void*);
size_t m = size / 2;
size_t n = size - size / 2;
struct dynarray *l;
create_dynarray(&l, m);
struct dynarray *r;
create_dynarray(&r, n);
for (size_t i = 0 ; i < m; i++) {
add_elem(l, param->memory[i]);
}
for (size_t j = m; j < size; j++) {
add_elem(r, param->memory[j]);
}
dynarray_mergesort(l);
dynarray_mergesort(r);
dynarray_merge(param, l, r, size);
}
return param;
}
//function used to mergesort the array
void *dynarray_merge(dynarray *param, dynarray *l, dynarray *r, int size) {
int i = 0,j = 0, k = 0;
while (i < size/2 && j < size - size / 2) {
if (l->memory[i] < r->memory[j]) {
param->memory[k] = l->memory[i];
i++;
k++;
} else {
param->memory[k] = r->memory[j];
j++;
k++;
}
}
while (i < size / 2) {
param->memory[k] = l->memory[i];
i++;
k++;
}
while (j < size - size / 2) {
param->memory[k] = r->memory[j];
j++;
k++;
}
return param;
}
When I call the function on an array such as [18, 14, 20, 16, 12] I get the same identical array. I have tried adding some printf()
in the mergesort function and I discovered that it seems to slice the array correctly. So the problem must be in the dynarray_merge() function. The way I am checking if an element in the first array is greater than an element of the other one seems right to me, so I am totally stuck.
I am posting a compilable example of my code, to show you better what I mean.
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
typedef struct dynarray {
void **memory;
size_t allocated;
size_t used;
int index;
} dynarray;
//creates a new, empty, dynarray
void create_dynarray(dynarray **array, size_t size) {
*array = calloc(size, sizeof **array);
(*array)->memory = NULL;
(*array)->allocated = 0;
(*array)->used = 0;
(*array)->index = -1;
}
//adds a new element at the bottom of dynarray
void add_elem(dynarray *array, void *data) {
size_t toallocate;
size_t size = sizeof(void *);
if ((array->allocated - array->used) < size) { // if M - N ...
toallocate = array->allocated == 0 ? size : (array->allocated * 2);
array->memory = realloc(array->memory, toallocate);
array->allocated = toallocate;
}
array->memory[++array->index] = data;
array->used = array->used + size;
}
//get length of the dynarray
int dynarray_length(dynarray *array) {
return array->index + 1;
}
//retrieves an element in a specific position of the dynarray
void *get_i_elem(dynarray *array, int index) {
if (index < 0 || index > array->index)
return NULL;
return array->memory[index];
}
//function used to mergesort the array
void *dynarray_merge(dynarray *param, dynarray *l, dynarray *r, int size) {
int i = 0,j = 0, k = 0;
while (i < size/2 && j < size - size / 2) {
if (l->memory[i] < r->memory[j]) {
param->memory[k] = l->memory[i];
i++;
k++;
} else {
param->memory[k] = r->memory[j];
j++;
k++;
}
}
while (i < size / 2) {
param->memory[k] = l->memory[i];
i++;
k++;
}
while (j < size - size / 2) {
param->memory[k] = r->memory[j];
j++;
k++;
}
return param;
}
//function used to slice the dynarray in two subarrays and call merge function
void *dynarray_mergesort(dynarray *param) {
if (dynarray_length(param) > 1) {
size_t size = param->used / sizeof(void*);
size_t m = size / 2;
size_t n = size - size / 2;
struct dynarray *l;
create_dynarray(&l, m);
struct dynarray *r;
create_dynarray(&r, n);
for (size_t i = 0 ; i < m; i++) {
add_elem(l, param->memory[i]);
}
for (size_t j = m; j < size; j++) {
add_elem(r, param->memory[j]);
}
dynarray_mergesort(l);
dynarray_mergesort(r);
dynarray_merge(param, l, r, size);
}
return param;
}
//print arrays, useful to test
void print_array(dynarray *array) {
for (int i = 0; i < dynarray_length(array); i++) {
printf("%d\t", *(int *)get_i_elem(array, i));
//puts("");
}
}
int main() {
struct dynarray *a;
create_dynarray(&a, 5);
int arr[5] = { 18, 14, 20, 16, 12};
int *ap = malloc(sizeof(int));
int *bp = malloc(sizeof(int));
int *cp = malloc(sizeof(int));
int *dp = malloc(sizeof(int));
int *ep = malloc(sizeof(int));
*ap = arr[0];
*bp = arr[1];
*cp = arr[2];
*dp = arr[3];
*ep = arr[4];
add_elem(a, ap);
add_elem(a, bp);
add_elem(a, cp);
add_elem(a, dp);
add_elem(a, ep);
printf("\nbefore mergesort\n");
print_array(a);
puts("");
printf("\nafter mergesort\n");
dynarray_mergesort(a);
print_array(a);
}
The problem is how you compare the elements:
if (l->memory[i] < r->memory[j]) ...
Here, you are comparing pointers, not the values pointed to. You get these pointers from the malloc calls in main, which gave you ascending addresses.
Your implenentation of the dynamic array uses void * as type for the elements, so that it can be used for elements of any type. Your merge sort doesn't know which type is used and therefore can't know how to compare.
You can provide a callback function to the sorting function like qsort does:
typedef int VoidPointerCmp(const void *a, const void *b);
Then pass this function to your two merge sort functions:
void *dynarray_mergesort(dynarray *param, , VoidPointerCmp *cmp) ...
and compare your items like so:
if (cmp(l->memory[i], r->memory[j]) < 0) ...
A suitable comparison function for integers could look like this:
int int_cmp(const void *pa, const void *pb)
{
const int *a = pa;
const int *b = pb;
if (*a < *b) return -1;
if (*a > *b) return 1;
return 0;
}
In main, call:
dynarray_mergesort(a, int_cmp);
Some notes:
You allocate memory for each of the entries. This is not necessary. You can make the void pointers point at the elements of the existing arrays:
for (int i = 0; i < 5; i++) add_elem(a, &arr[i]);
Sorting will not affect the original array arr.
You don't free the allocated memory or destroy your dynamic arrays. You should probably write a destructor function for the dynamic arrays and clean up the temporary arrays l and r after using them.
I think that you don't use the used field correctly. You probably don't need it: It is enough to have the allocated size and current length of the array. (You can replace the index with the length.)
Related
I have this project where I need to create an array, split it into two, and then sort each array using its own thread, then merge the two arrays back into a single array. Thankfully, the final array doesn't need to be sorted itself.
I wrote this code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
//Change length of main input array
const int arrLen = 20;
const int mid = arrLen/2;
void swap(int *a, int *b){
int temp = *a;
*a = *b;
*b = temp;
}
void *sort(void *arg){
int min_pos;
for(int a=0; a<arrLen-1; a++){
min_pos = a;
for(int b=a+1; b<arrLen; b++){
if(array[b] < array[min_pos]){
min_pos = b;
}
}
swap(&array[min_pos], &array[a]);
}
}
void printResult(int array[]){
for(int k=0; k<arrLen/2; k++){
printf("Num%d: %d\n", k, array[k]);
}
}
//Adds random values to each position in a given array
void arrayCreator(int array[]){
int random;
for(int i=0; i<arrLen; i++){
random = rand() % 100;
array[i] = random;
}
}
void main(){
printf("Program Initialized...\n");
int mainArray [arrLen], firstHalf[mid], secondHalf[mid], random;
time_t t;
srand((unsigned) time(&t));
//Populate mainArray
for(int i=0; i<arrLen; i++){
mainArray[i] = rand()%100;
}
printf("Array created...\n");
//Split mainArray[] into two seperate arrays
for(int p=0; p<arrLen; p++){
if(p<mid){
firstHalf[p]=mainArray[p];
}
else{
secondHalf[p-mid]=mainArray[p];
}
}
pthread_t tid1;
pthread_t tid2;
pthread_create(&tid1, NULL, sort, (void*)firstHalf);
//pthread_create(&tid2, NULL, sort, secondHalf);
printResult(firstHalf);
//printResult(secondHalf);
}
How can I get the sort function to access my array?
First, I would look at the signature of the standard function qsort and provide the same interface for your sort function. I'd also rename it into bubble_sort since it's doing bubble sorting. Your function would then look like this:
void bubble_sort(void *ptr, size_t arrLen, size_t elem_size,
int (*comp)(const void *, const void *)) {
char *array = ptr; // to be able to do pointer arithmetic
for (size_t a = 0; a < arrLen - 1; a++) {
for (size_t b = a + 1; b < arrLen; b++) {
if (comp(array + a * elem_size, array + b * elem_size) > 0) {
swap(array + a * elem_size, array + b * elem_size, elem_size);
}
}
}
}
elem_size is the size of the elements you want to sort, which will be set to sizeof(int) later.
comp is a function taking two void* and returns an int where ...
< 0 means *a < *b
> 0 means *a > *b
== 0 means *a == *b
swap also takes the elem_size parameter to be able to swap objects of any size. Implemented, it could look like this:
void swap(void *a, void *b, size_t elem_size) {
char tmp[elem_size];
memcpy(tmp, a, elem_size);
memcpy(a, b, elem_size);
memcpy(b, tmp, elem_size);
}
Now, you can't start a thread by calling bubble_sort directly. A thread start routine only takes a single void* as an argument so we need some way of passing all the necessary arguments on. We can package them in a struct like this:
struct sort_thread_args {
pthread_t tid;
void *array;
size_t arrLen;
size_t elem_size;
int (*comp_func)(const void *, const void *);
};
void *sort_thread(void *arg) { // the thread start routine
struct sort_thread_args *ta = arg;
bubble_sort(ta->array, ta->arrLen, ta->elem_size, ta->comp_func);
return NULL;
}
As can be seen above, the struct contains all the necessary information to call bubble_sort + the thread id, which I've stored there for convenience only. It's not needed by the thread itself.
The above functions are type agnostic so you can use these to sort any contiguous array, either by calling bubble_sort directly or in a thread.
Since you'll be using this to sort arrays of int, we need a comparison function for ints which does what was described above:
int comp_int(const void *va, const void *vb) {
const int *a = va;
const int *b = vb;
if (*a < *b) return -1;
if (*a > *b) return 1;
return 0;
}
Then for sorting half the array in one thread and the other half in another thread, I suggest that you don't copy the contents of mainArray to new arrays. Just leave the content in mainArray and tell each thread where to start sorting and how many elements it should sort.
Example:
#define Size(x) (sizeof(x) / sizeof *(x))
int main() {
srand((unsigned)time(NULL));
puts("Program Initialized...");
int arrLen = 35; // just an example to show that it handes uneven amounts too
int mainArray[arrLen];
// Populate mainArray
for (size_t i = 0; i < Size(mainArray); i++) {
mainArray[i] = rand() % 100;
}
puts("Array created...");
// print the created array
for (size_t i = 0; i < Size(mainArray); ++i) printf("%2d ", mainArray[i]);
putchar('\n');
puts("Running sorting threads...");
struct sort_thread_args ta[2]; // 2 for 2 threads
for (size_t i = 0; i < Size(ta); ++i) {
// element index for where this thread should start sorting:
size_t base = i * Size(mainArray) / Size(ta);
// prepare the arguments for this thread:
ta[i] = (struct sort_thread_args) {
.array = mainArray + base, // points at the first element for this thread
.arrLen = (i + 1) * Size(mainArray) / Size(ta) - base,
.elem_size = sizeof(int),
.comp_func = comp_int // the comparison function we made above
};
// and start the thread:
pthread_create(&ta[i].tid, NULL, sort_thread, &ta[i]);
}
puts("Joining threads");
for (size_t i = 0; i < Size(ta); ++i) {
pthread_join(ta[i].tid, NULL);
}
puts("Result:");
for (size_t i = 0; i < Size(mainArray); ++i) printf("%2d ", mainArray[i]);
putchar('\n');
}
Demo
Simply assign arg to your array variable in order to convert from void * to int *.
void *sort(void *arg){
int min_pos;
int *array = arg;
for(int a=0; a<arrLen-1; a++){
min_pos = a;
for(int b=a+1; b<arrLen; b++){
if(array[b] < array[min_pos]){
min_pos = b;
}
}
swap(&array[min_pos], &array[a]);
}
}
I'm having trouble in adding integers to a dynamic array reader, so the second function in the code below. Why do the added numbers in the output look weird? I'm guessing there's a memory problem, I'm somehow allocating new memory incorrectly.
int *create_dyn_array(unsigned int n)
{
int *array = malloc(n * sizeof(*array));
for (size_t i = 0; i < n; i++) {
scanf("%d", &array[i]);
}
return array;
}
int *add_dyn_array(int *arr, unsigned int num, int newval)
{
int *temp = NULL;
temp = realloc(arr, (num + 1)*sizeof(int));
arr = temp;
int *newarray = arr;
while(*arr) {
arr++;
}
int testarray[1];
int *ptr = testarray;
int j = 1;
while (j > 0) {
*ptr = newval;
ptr++;
j--;
}
ptr = testarray;
while(*ptr) {
*arr++ = *ptr++;
}
return newarray;
}
void printarray(const int *array, int size) {
printf("{ ");
for (int i = 0; i < size; ++i) {
printf("%d, ", array[i]);
}
printf(" }\n");
}
int main()
{
int *array = create_dyn_array(5);
printarray(array, 5);
array = add_dyn_array(array, 5, 10);
printarray(array, 6);
array = add_dyn_array(array, 6, 100);
printarray(array, 7);
array = add_dyn_array(array, 7, 1000);
printarray(array, 8);
return 0;
}
What is wrong in the realloc logic?
This is how realloc works:
The contents of the new object shall be the same as that of the old object prior to deallocation, up to the lesser of the new and old sizes.
No need to copy anything manually. The whole function can be reduced to this:
int *add_dyn_array(int *arr, unsigned int num, int newval)
{
int *temp = realloc(arr, (num + 1)*sizeof(int));
if(temp == NULL)
{
/* optionally handle errors in some way */
exit(EXIT_FAILURE);
}
temp[num] = newval;
return temp;
}
Please note however that reallocing one single item at a time is very inefficient.
I was trying to implement merge sort in C.
But when I test the code I encounter this error c0000374 in my merge sort function when I try to split array into left right array.
The code is as follows.
typedef struct EntryStruct {
int data;
char *name;
} Entry;
typedef char *String;
void merge(Entry *output, Entry *L, int nL, Entry *R, int nR) {
int i = 0;
int j = 0;
int k = 0;
while (k < nL + nR) {
if ((L[i].data != NULL && L[i].data < R[i].data) || R[j].data == NULL) {
output[k] = L[i];
i++;
} else {
output[k] = R[j];
j++;
}
k++;
}
}
void merge_sort(Entry *entries, int n) {
if (n > 1) {
int mid = n / 2;
Entry *temp = (Entry *)malloc(n * sizeof(Entry));
Entry *left = (Entry *)malloc(mid * sizeof(Entry));
Entry *right = (Entry *)malloc((n - mid) * sizeof(Entry));
for (int l = 0; l < mid; l++)
left[l] = entries[l];
for (int r = mid; r < n; r++)
right[r] = entries[r];
merge_sort(left, mid);
merge_sort(right, n - mid);
merge(temp, left, mid, right, n - mid);
for (int i = 0 ; i < n; i++) {
entries[i] = temp[i];
}
free(temp);
}
}
Entry Entry_create(int data, String name) {
Entry node;
node.name = (String)malloc(strlen(name) + 1);
strcpy(node.name, name);
node.data = data;
return node;
}
void printArrByName(Entry *arr, int s) {
for (int i = 0; i < s; i++) {
printf("%s\n", arr[i].name);
}
}
int main(void) {
Entry *arr = malloc(5 * sizeof(*arr));
arr[0] = Entry_create(5, "abc");
arr[1] = Entry_create(6, "def");
arr[2] = Entry_create(2, "ghijk");
arr[3] = Entry_create(3, "ksdljf");
arr[4] = Entry_create(1, "lsdfjl");
merge_sort(arr, 5);
printArrByName(arr, 5);
free(arr);
}
I want to ask what is the cause of this problem in my case and how to solve it.
Is this happen because I split array in to left right in the wrong way or is it something to do with the initialization of the array.
There are multiple problems in the code causing undefined behavior:
[major: undefined behavior] In the merge_sort function, the loop for (int r = mid; r < n; r++) right[r] = entries[r]; accesses the array pointed to by right beyond the end. You should write:
for (int r = mid; r < n; r++)
right[r - mid] = entries[r];
This bug is a good candidate to explain the observed behavior as it corrupts the malloc() internal data, causing a subsequent call to malloc() to crash.
[major: memory leak] You do not free left, nor right. As a matter of fact, allocating copies of the left and right parts of the array is not even necessary.
[major: undefined behavior] In the merge function, you do not test if i is less than nL, nor of j is less than nR before accessing L[i] or R[j]. Testing if the data member is not NULL does not suffice, accessing an element beyond the end of an array has undefined behavior.
[minor: unstable sort] L[i].data < R[i].data might not preserve the order of entries that have the same data value. You should use L[i].data <= R[i].data to implement stable sorting.
[hint] Defining typedef char *String; is a bad idea. Do not hide pointers behind typedefs, it is confusing and error prone.
Here is a modified version:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct EntryStruct {
int data;
char *name;
} Entry;
#ifdef _MSC_VER
// define strdup on legacy systems
char *strdup(const char *s) {
size_t len = strlen(s);
char *p = (char *)malloc(len + 1);
if (p)
memcpy(p, s, len + 1);
return p;
}
#endif
void merge(Entry *output, Entry *L, int nL, Entry *R, int nR) {
int i = 0;
int j = 0;
int k = 0;
while (k < nL + nR) {
if (i < nL && (j >= nR || L[i].data <= R[j].data)) {
output[k] = L[i];
i++;
} else {
output[k] = R[j];
j++;
}
k++;
}
}
void merge_sort(Entry *entries, int n) {
if (n > 1) {
int mid = n / 2;
Entry *temp;
Entry *left = entries;
Entry *right = entries + mid;
merge_sort(left, mid);
merge_sort(right, n - mid);
temp = (Entry *)malloc(n * sizeof(Entry));
merge(temp, left, mid, right, n - mid);
for (int i = 0; i < n; i++) {
entries[i] = temp[i];
}
free(temp);
}
}
Entry Entry_create(int data, const char *name) {
Entry node;
node.name = strdup(name);
node.data = data;
return node;
}
void printArrByName(Entry *arr, int n) {
for (int i = 0; i < n; i++) {
printf("%s\n", arr[i].name);
}
}
int main(void) {
Entry *arr = malloc(5 * sizeof(*arr));
arr[0] = Entry_create(5, "abc");
arr[1] = Entry_create(6, "def");
arr[2] = Entry_create(2, "ghijk");
arr[3] = Entry_create(3, "ksdljf");
arr[4] = Entry_create(1, "lsdfjl");
merge_sort(arr, 5);
printArrByName(arr, 5);
for (int i = 0; i < 5; i++)
free(arr[i].name);
free(arr);
return 0;
}
Although not needed for small arrays, and since there are answers based on the questions code, here is a somewhat optimized top down merge sort that avoids copy backs by using a pair of mutually recursive functions (...a2a, ...a2b). An entry function does a one time allocation of the temporary array. On my system, it takes less than .5 second to sort an array of 4 million structures.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct EntryStruct {
int data;
char *name;
} Entry;
/* prototypes for mutually recursive functions */
void merge_sort_a2a(Entry *a, Entry *b, int ll, int ee);
void merge_sort_a2b(Entry *a, Entry *b, int ll, int ee);
void merge(Entry *a, Entry *b, int ll, int rr, int ee)
{
int o = ll; /* b[] index */
int l = ll; /* a[] left index */
int r = rr; /* a[] right index */
while(1){
if(a[l].data <= a[r].data){ /* if a[l] <= a[r] */
b[o++] = a[l++]; /* copy a[l] */
if(l < rr) /* if not end of left run */
continue; /* continue (back to while) */
while(r < ee) /* else copy rest of right run */
b[o++] = a[r++];
break; /* and return */
} else { /* else a[l] > a[r] */
b[o++] = a[r++]; /* copy a[r] */
if(r < ee) /* if not end of right run */
continue; /* continue (back to while) */
while(l < rr) /* else copy rest of left run */
b[o++] = a[l++];
break; /* and return */
}
}
}
void merge_sort_a2a(Entry *a, Entry *b, int ll, int ee)
{
int rr;
if(ee - ll < 2){ /* if 1 element */
return; /* return */
}
rr = ll + (ee-ll)/2; /* mid point, start of right run */
merge_sort_a2b(a, b, ll, rr);
merge_sort_a2b(a, b, rr, ee);
merge(b, a, ll, rr, ee);
}
void merge_sort_a2b(Entry *a, Entry *b, int ll, int ee)
{
int rr;
if(ee - ll < 2){ /* if 1 element */
b[ll] = a[ll]; /* copy to b */
return;
}
rr = ll + (ee-ll)/2; /* mid point, start of right run */
merge_sort_a2a(a, b, ll, rr);
merge_sort_a2a(a, b, rr, ee);
merge(a, b, ll, rr, ee);
}
void merge_sort(Entry *a, int n) {
if(n < 2)
return;
Entry *b = malloc(n * sizeof(Entry));
merge_sort_a2a(a, b, 0, n);
free(b);
}
Entry Entry_create(int data, const char *name) {
Entry node;
node.name = _strdup(name); /* _strdup is ISO name */
node.data = data;
return node;
}
void printArrByName(Entry *arr, int n) {
for (int i = 0; i < n; i++) {
printf("%s\n", arr[i].name);
}
}
int main(void) {
Entry *arr = malloc(5 * sizeof(*arr));
arr[0] = Entry_create(5, "abc");
arr[1] = Entry_create(6, "def");
arr[2] = Entry_create(2, "ghijk");
arr[3] = Entry_create(3, "ksdljf");
arr[4] = Entry_create(1, "lsdfjl");
merge_sort(arr, 5);
printArrByName(arr, 5);
for (int i = 0; i < 5; i++)
free(arr[i].name);
free(arr);
return 0;
}
I'm trying to implement the mergesort algorithm using a dynamic array structure in c, but when i call the function to split the original array instead of getting two subarrays i get a seg fault error.
I'm pretty sure it has something to deal with how i define the size of my structure, but i cannot get over it. Here's how i've defined my structure and how i create and initialize it:
typedef struct dynarray
{
void **memory;
size_t allocated; //total size of the array
size_t used; //used size of the array
int index;
} dynarray;
//creates a new, empty, dynarray
void create_dynarray(dynarray **array, size_t size)
{
*array = calloc(size, sizeof(array));
(*array)->memory = NULL;
(*array)->allocated = 0;
(*array)->used = 0;
(*array)->index = -1;
}
This how i've defined my mergesort functions
//function used to slice the dynarray in two subarrays and call merge function
void* dynarray_mergesort(dynarray *param){
if(dynarray_length(param)>1){
param->index = 0;
printf("index of first:%d\t", param->index);
size_t size = param->used;
size_t m = size/2;
size_t n = size - size/2;
struct dynarray *l;
create_dynarray(&l, m);
printf("index of left:%d\t", l->index);
struct dynarray *r;
create_dynarray(&r, n);
printf("index of right:%d\n", r->index);
for(int i = 0 ; i < m; i++){
add_elem(l, param->memory[i]);
}for(int j = m; j < n; j++){
add_elem(r, param->memory[j]);
}
puts("first");
print_array(l);
puts("second");
print_array(r);
dynarray_mergesort(l);
dynarray_mergesort(r);
//dynarray_merge(param, l , r, size);
}
return param;
}
//function used to mergesort the array
void* dynarray_merge(dynarray *param, dynarray *l, dynarray *r, int size){
int i,j,k;
while(i < size/2 && j < size-size/2){
if(l->memory[i] < r->memory[j]){
param->memory[k] = l->memory[i];
i++;
k++;
}else{
param->memory[k] = r->memory[j];
j++;
k++;
}
}
while(i < size/2)
param->memory[k++] = l->memory[i++];
}while(j < size-size/2){
param->memory[k++] = r->memory[j++];
}
return param;
}
//function used to mergesort the array
void* dynarray_merge(dynarray *param, dynarray *l, dynarray *r, int size){
int i,j,k;
while(i < size/2 && j < size-size/2){
if(l->memory[i] < r->memory[j]){
param->memory[k] = l->memory[i];
i++;
k++;
}else{
param->memory[k] = r->memory[j];
j++;
k++;
}
}
while(i < size/2){
param->memory[k++] = l->memory[i++];
}while(j < size-size/2){
param->memory[k++] = r->memory[j++];
}
return param;
}
Probably i'm confused on how the size of my dynamic array is defined and how i have to treat it in my functions. Here's a compilable example to help you understand the problem. It is pretty long but most functions can be ignored as they are utility functions and they seem to work good. The problem is located in the mergesort function, but i'm afraid it could be related on how i've defined my dynarraystructure.
Ps. the line calling the dynarray_merge(param, l , r, size); is commented because i'm working on problems located in the dynarray_mergesort(dynarray *param);
Ps2: the printf functions called inside the dynarray_mergesort(dynarray *param); are used as debugging infos.
#include<stdio.h>
#include<stdlib.h>
typedef struct dynarray
{
void **memory;
size_t allocated;
size_t used;
int index;
} dynarray;
//get length of the dynarray
int dynarray_length(dynarray *array)
{
return array->index + 1;
}
//retrieves an element in a specific position of the dynarray
void* get_i_elem(dynarray *array,int index)
{
if (index < 0 || index > array->index) return NULL;
return array->memory[index];
}
//print arrays, useful to test
void print_array(dynarray *array)
{
for(int i = 0; i < dynarray_length(array); i++) {
printf("%d\t", *(int *)get_i_elem(array, i));
//puts("");
}
}
//creates a new, empty, dynarray
void create_dynarray(dynarray **array, size_t size)
{
*array = calloc(size, sizeof(array));
(*array)->memory = NULL;
(*array)->allocated = 0;
(*array)->used = 0;
(*array)->index = -1;
}
//adds a new element at the bottom of dynarray
void add_elem(dynarray *array, void *data)
{
size_t toallocate;
size_t size = sizeof(void *);
if ((array->allocated - array->used) < size){ // if M - N ...
toallocate = array->allocated == 0 ? size : (array->allocated * 2);
array->memory = realloc(array->memory, toallocate);
array->allocated = toallocate;
}
array->memory[++array->index] = data;
array->used = array->used + size;
}
//function used to slice the dynarray in two subarrays and call merge function
void* dynarray_mergesort(dynarray *param){
if(dynarray_length(param)>1){
param->index = 0;
printf("index of first:%d\t", param->index);
size_t size = param->used;
size_t m = size/2;
size_t n = size - size/2;
struct dynarray *l;
create_dynarray(&l, m);
printf("index of left:%d\t", l->index);
struct dynarray *r;
create_dynarray(&r, n);
printf("index of right:%d\n", r->index);
for(int i = 0 ; i < m; i++){
add_elem(l, param->memory[i]);
}for(int j = m; j < n; j++){
add_elem(r, param->memory[j]);
}
puts("first");
print_array(l);
puts("second");
print_array(r);
dynarray_mergesort(l);
dynarray_mergesort(r);
//dynarray_merge(param, l , r, size);
}
return param;
}
//function used to mergesort the array
void* dynarray_merge(dynarray *param, dynarray *l, dynarray *r, int size){
int i,j,k;
while(i < size/2 && j < size-size/2){
if(l->memory[i] < r->memory[j]){
param->memory[k] = l->memory[i];
i++;
k++;
}else{
param->memory[k] = r->memory[j];
j++;
k++;
}
}
while(i < size/2){
param->memory[k++] = l->memory[i++];
}while(j < size-size/2){
param->memory[k++] = r->memory[j++];
}
return param;
}
int main(){
struct dynarray *a;
create_dynarray(&a, 5);
int arr[5] = {18,14, 20,16,12};
int *ap = malloc(sizeof(int));
int *bp = malloc(sizeof(int));
int *cp = malloc(sizeof(int));
int *dp = malloc(sizeof(int));
int *ep = malloc(sizeof(int));
*ap = arr[0];
*bp = arr[1];
*cp = arr[2];
*dp = arr[3];
*ep = arr[4];
add_elem(a, ap);
add_elem(a, bp);
add_elem(a, cp);
add_elem(a, dp);
add_elem(a, ep);
dynarray_mergesort(a);
print_array(a);
}
In addition to the allocation shortfall mentioned in the comments below your question (e.g. needing *array = calloc(size, sizeof **array);), you have a simple error leading to your SegFault (you have other errors as well). You are storing the number of bytes in the size variable in dynarray_mergesort, not the number of pointers. So in dynarray_mergesort when you declare size_t size = param->used; your value of size is multiple of sizeof(void*) (e.g. sizeof(a_pointer)) times the number of pointers you have actually used. This leads to incorrect values for m and n.
To remedy the problem, you can simply do:
size_t size = param->used / sizeof(void*);
You have another error with your loop limits in:
for(size_t j = m; j < n; j++){
add_elem(r, param->memory[j]);
}
Where m = size/2; and n = size - size/2;. You actually need your limits of m -> size, e.g.:
for(size_t j = m; j < size; j++){
add_elem(r, param->memory[j]);
}
(note: above the proper type for i and j are both size_t to correspond to m and n and prevent "comparison between signed and unsigned integer expressions")
As noted in my comment, you have uninitialized value problems in dynarray_merge. You need to initialize i and k, e.g.
int i=0, j=0, k=0;
before you attempt:
i++;
k++;
With those changes your code runs to the end without problems (other than leaking memory):
$ ./bin/dynarraymergeorig
index of first:0 index of left:-1 index of right:-1
first
18 14
second
20 16 12
index of first:0 index of left:-1 index of right:-1
first
18
second
14
index of first:0 index of left:-1 index of right:-1
first
20
second
16 12
index of first:0 index of left:-1 index of right:-1
first
16
second
12
18
You still have problems merging your list (that is left to you to further investigate), but your SegFault issue is solved. Let me know if you have further questions. (other than on the changes required to fix your merge algorithm left to you)
The inside of the create_dynarray function
*array = calloc(size, sizeof(array));
should be changed to:
*array = calloc(size, sizeof(**array))
to do what you actually want to do (allocate a memory for the array with an element size dynarray * size).
The exercise, that I have to complete says:
That array_remove function must remove from the array arr the value, that is in the position pos, and scale of a position successive values of pos, and eventually change the array size for no gaps.
If this value is not included in the array (if pos is greater than pn (array size)), then you should not do anything.
My problem is:
Probably very wrong to use the malloc function, because when it is performed, it shows the following error:
MAIN.C:
#include "array.h"
int main(void)
{
double arr[] = { 1.0,2.0,3.0,4.0,5.0 };
size_t pn = 5;/*array length*/
size_t pos = 2;/*position of the number to be deleted*/
array_remove(arr, &pn, pos);
}
ARRAY.C:
#include "array.h"
void array_remove(double *arr, size_t *pn, size_t pos)
{
int x = *pn;
int y = pos;
if (x > y)
{
for (int i = y; i < x; i++)
{
arr[i] = arr[i + 1];
}
realloc(&arr, sizeof(double) * 4);
}
}
According to the C docs:
realloc Reallocates the given area of memory that must be previously allocated
by malloc(), calloc() or realloc() and not yet freed with free,
otherwise, the results are undefined.
You have an out of bound problem as well at the following lines when i=x-1 you try to access at arr[i+1] = arr[x=pn]:
for (int i = y; i < ; i++) {
arr[i] = arr[i + 1];
Check the following code out *(live: https://ideone.com/mbSzjL
#include<stdlib.h>
void array_remove(double **arr, int *pn, int pos) {
int x = *pn;
int y = pos;
if (x > y) {
//check if after deletion size is zero!
if (x > y) {
for (int i = y; i < x-1; i++) {
(*arr)[i] = (*arr)[i + 1];
}
*arr=realloc(*arr, sizeof(double) * x-1);
*pn=*pn-1;
}
}
}
int main(void) {
int pn = 20;/*array length*/
int pos = 5;/*position of the number to be deleted*/
double *arr = malloc(sizeof(double)*pn);
printf("%p\n",arr);
for(int i=0;i<pn;i++){
arr[i] = i;
}
for(int i=0;i<pn;i++){
printf("%.f ",arr[i]);
}
printf("\n");
printf("%i\n",pn);
array_remove(&arr, &pn, pos);
printf("%p\n",arr);
for(int i=0;i<pn;i++){
printf("%.f ",arr[i]);
}
printf("\n");
printf("%i",pn);
free(arr);
}
Don't forget to realloc using the right size (not using an hardcoded 4) and check for the edge case in which size is zero after deletion!
In addition,
free the memory at the end and to update the size variable.
http://en.cppreference.com/w/c/memory/realloc
arr array is stack allocated. You cannot realloc something that wasn't mallocated.
You probably want something like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
bool array_remove(double **arr, size_t *pn, size_t pos)
{
int x = *pn - 1;
int y = pos;
int i;
double *temp;
if (x > y) {
for (i = y; i < x; i++) {
(*arr)[i] = (*arr)[i + 1];
}
temp = realloc(*arr, sizeof(double) * x);
}
if (arr != NULL)
{
*arr = temp;
*pn -=1;
return true;
}
else
{
return false;
}
}
int main(void)
{
size_t pn = 5; // array length
size_t pos = 2; // position of the number to be deleted
int i;
double *arr = malloc(pn*sizeof(double));
if (arr != NULL)
{
for (i=0; i<pn; i++)
{
arr[i] = (double)(i+1);
}
if (array_remove(&arr, &pn, pos) == false)
{
printf("Failed to remove element %zu\n", pos);
}
for (i=0; i<pn; i++)
printf ("arr[%d]: %f\n", i, arr[i]);
free(arr);
}
else
{
printf("Failed to alloc array\n");
}
return 0;
}
As you can see I changed the loop of array_remove. In your code you are addressing the array out of bound on the last loop, because of i=4 and then:
arr[i] = arr[i + 1]; is arr[4] = arr[5]
Indexes of a 5 elements array start from 0 to 4.
actually you have a different problem here:
int x = *pn; //x=5
int y = pos; //y=2
if (x > y) {
for (int i = y; i < x; i++) {
arr[i] = arr[i + 1];
}
On the last iteration, you do
arr[4] = arr[5]
This is out of range addressig and that's probably your problem, or at least your first one.
Also, even though it's not technically wrong it's conceptually wrong:
array_remove(arr, &pn, pos);
Never pass a value by pointer unless you plan on modifying it. Not the case here, so you can pass it by value.