Code:
#include <stdio.h>
#include <stdbool.h>
#include "project1.h"
int main()
{
int i, r, NUM_ITEMS, t, ret, ava;
int* numbers;
NUM_ITEMS = 0;
ava = 100;
numbers = malloc(100 * sizeof(int));
while(((ret = scanf("%d",&t)) != EOF) && (ret != 0))
{
if (NUM_ITEMS <= ava)
{
numbers[NUM_ITEMS] = t;
NUM_ITEMS++;
} else
{
numbers = realloc(numbers, sizeof(numbers) + sizeof(int)*100);
ava = ava + 100 ;
printf("%d\n",t);
numbers[NUM_ITEMS] = t;
printf("\t Array at %d = %d\n",NUM_ITEMS, numbers[NUM_ITEMS]);
NUM_ITEMS++;
}
}
r = bubbleSort(numbers, NUM_ITEMS);
r = insertionSort(numbers, NUM_ITEMS);
}
I am doing a proj for class. the point is to make some sort function and let them have at. The problem is I am having issues with malloc. I though I understood the memory alloction stuff pretty well but when ever I try to run this on a file with alot of numbers (say 100000) I run into memory issues and the computer I am compiling on gets all upset. I have the sorts running and sorting the same number of element when I use a a size I define in the code.
So, my question is, is my problem a result of the way I am mallocing here or should I look at me calloc call in my sorts or is it the computer?
Here is an example of a sort
Code:
/*********************************************************
int bubbleSort(int * a, int n);
*********************************************************/
int bubbleSort(int * number, int array_size)
{
int i, j, r, temp;
int * array_sort;
//Allocating array of same size as passed array.
array_sort = (int *)calloc(array_size,sizeof (int));
if(array_sort != NULL)
{
//printf("Array created of size \n",array_size);
}
else
{
//printf("Not enough memory\n");
exit(1);
}
// Bubble Code
// Clean-up
free (array_sort);
return(0);
}
Thanks for any suggestion