L
lancer6238
Hi,
I'm writing a program that separates a set of integers into groups and
then quicksort each group individually. However, I'm having problems
with my realloc() function. (Pardon me if the indentation is weird.
There's no preview button here and I can't tell if my indentation is
correct.)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 10 // number of integers to sort
#define size 2 // number of groups
#define MAX 100 // maximum value of the integers
void quicksort(int a[], int lo, int hi)
{
int h, l, p, t;
if (lo < hi)
{
l = lo;
h = hi;
p = a[hi];
do {
while ((l < h) && (a[l] <= p))
l = l+1;
while ((h > l) && (a[h] >= p))
h = h-1;
if (l < h)
{
t = a[l];
a[l] = a[h];
a[h] = t;
}
} while (l < h);
t = a[l];
a[l] = a[hi];
a[hi] = t;
quicksort(a, lo, l-1);
quicksort(a, l+1, hi);
}
}
int main()
{
int i, j, *length, x[N], **a, *temp;
srand(time(NULL));
a = (int **)malloc(size * sizeof(int*));
for (i = 0 ; i < size ; i++)
a = (int*)malloc((N/size + 1) * sizeof(int));
length = (int *)malloc(size * sizeof(int));
/* Generate random integers */
printf("Original: \n");
for (i = 0 ; i < N ; i++)
{
x = rand() % MAX;
printf("%d ", x);
}
printf("\n\n");
for (j = 0 ; j < N ; j++)
{
for (i = size ; i >= 1 ; i--)
{
if ((double)x[j]/(double)MAX > (double)(i-1)/
(double)size)
{
a[i-1][length[i-1]] = x[j];
length[i-1]++;
if (length[i-1] > N/size + 1)
{
if ((temp = realloc(a[i-1], sizeof(int) * 2 * (N/size
+ 1))) == NULL)
{
printf("ERROR: realloc failed");
exit(0);
}
a[i-1] = temp;
}
break;
}
}
}
for(i = 0 ; i < size ; i++)
quicksort(a,0,length-1);
printf("Sorted:\n");
for(i = 0 ; i < size ; i++)
{
for (j = 0 ; j < length ; j++)
printf("%d ", a[j]);
}
printf("\n");
for (i = 0 ; i < length ; i++)
free(a);
free(a);
free(length);
return 0;
}
Everytime the realloc() function is needed, i.e. when the size of any
of the 2 groups has to be increased to more than the original 6
integers, I'll either get the error "Segmentation Fault", "*** glibc
detected *** realloc(): invalid next size: 0x00000000005020c0 ***", or
the value that's supposed to go into the newly created memory, e.g.
a[0][6], becomes 0.
How do I fix this problem?
Thank you.
Regards,
Rayne
I'm writing a program that separates a set of integers into groups and
then quicksort each group individually. However, I'm having problems
with my realloc() function. (Pardon me if the indentation is weird.
There's no preview button here and I can't tell if my indentation is
correct.)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 10 // number of integers to sort
#define size 2 // number of groups
#define MAX 100 // maximum value of the integers
void quicksort(int a[], int lo, int hi)
{
int h, l, p, t;
if (lo < hi)
{
l = lo;
h = hi;
p = a[hi];
do {
while ((l < h) && (a[l] <= p))
l = l+1;
while ((h > l) && (a[h] >= p))
h = h-1;
if (l < h)
{
t = a[l];
a[l] = a[h];
a[h] = t;
}
} while (l < h);
t = a[l];
a[l] = a[hi];
a[hi] = t;
quicksort(a, lo, l-1);
quicksort(a, l+1, hi);
}
}
int main()
{
int i, j, *length, x[N], **a, *temp;
srand(time(NULL));
a = (int **)malloc(size * sizeof(int*));
for (i = 0 ; i < size ; i++)
a = (int*)malloc((N/size + 1) * sizeof(int));
length = (int *)malloc(size * sizeof(int));
/* Generate random integers */
printf("Original: \n");
for (i = 0 ; i < N ; i++)
{
x = rand() % MAX;
printf("%d ", x);
}
printf("\n\n");
for (j = 0 ; j < N ; j++)
{
for (i = size ; i >= 1 ; i--)
{
if ((double)x[j]/(double)MAX > (double)(i-1)/
(double)size)
{
a[i-1][length[i-1]] = x[j];
length[i-1]++;
if (length[i-1] > N/size + 1)
{
if ((temp = realloc(a[i-1], sizeof(int) * 2 * (N/size
+ 1))) == NULL)
{
printf("ERROR: realloc failed");
exit(0);
}
a[i-1] = temp;
}
break;
}
}
}
for(i = 0 ; i < size ; i++)
quicksort(a,0,length-1);
printf("Sorted:\n");
for(i = 0 ; i < size ; i++)
{
for (j = 0 ; j < length ; j++)
printf("%d ", a[j]);
}
printf("\n");
for (i = 0 ; i < length ; i++)
free(a);
free(a);
free(length);
return 0;
}
Everytime the realloc() function is needed, i.e. when the size of any
of the 2 groups has to be increased to more than the original 6
integers, I'll either get the error "Segmentation Fault", "*** glibc
detected *** realloc(): invalid next size: 0x00000000005020c0 ***", or
the value that's supposed to go into the newly created memory, e.g.
a[0][6], becomes 0.
How do I fix this problem?
Thank you.
Regards,
Rayne