P
pereges
I'm trying to write a program that will create a dynamically growing
array. There is a parent array and from this I want to create a
seperate array with elements that are less than <= x (say some random
integer). Here's my program which compiles and exectures
successfully :
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int *parent;
int *child = NULL, *tempstore;
int child_size, i, parent_size;
printf("Enter size of parent array\n");
if(scanf("%d", &parent_size) != 1)
return (1);
parent = malloc(sizeof(int) * parent_size);
if(parent == NULL)
{
fprintf(stderr, "Memory allocation failed\n");
return (1);
}
for(i = 0; i < parent_size; i++)
{
printf("Enter element %d:", i);
if(scanf("%d", &parent) != 1)
return (1);
printf("\n");
}
child_size = 0;
for(i = 0; i < parent_size; i++)
{
if(parent <= 166) /* comparing against a random number 166 in
this eg. */
{
tempstore = realloc(child, (++child_size)*sizeof(int));
if(tempstore == NULL)
{
fprintf(stderr, "Memory reallocation failed\n");
return (1);
}
tempstore[child_size - 1] = parent;
child = tempstore;
}
}
for(i = 0; i < child_size; i++)
printf("%d\n", child);
return (0);
}
Another alternative is to walk the entire array and count the number
of child entries. Then allocate memory for the child array using the
number of child entries, again run through parent array , check for
the condition and put the element (which satisfies the condition) in
the child array. This requires two passes to be made on an array. What
I posted is just a sample code. In my actual project, there can be as
many as 10,000,000 entries in the parent array. Do you think going
through 10,000,000 entries twice is a better idea than using realloc ?
array. There is a parent array and from this I want to create a
seperate array with elements that are less than <= x (say some random
integer). Here's my program which compiles and exectures
successfully :
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int *parent;
int *child = NULL, *tempstore;
int child_size, i, parent_size;
printf("Enter size of parent array\n");
if(scanf("%d", &parent_size) != 1)
return (1);
parent = malloc(sizeof(int) * parent_size);
if(parent == NULL)
{
fprintf(stderr, "Memory allocation failed\n");
return (1);
}
for(i = 0; i < parent_size; i++)
{
printf("Enter element %d:", i);
if(scanf("%d", &parent) != 1)
return (1);
printf("\n");
}
child_size = 0;
for(i = 0; i < parent_size; i++)
{
if(parent <= 166) /* comparing against a random number 166 in
this eg. */
{
tempstore = realloc(child, (++child_size)*sizeof(int));
if(tempstore == NULL)
{
fprintf(stderr, "Memory reallocation failed\n");
return (1);
}
tempstore[child_size - 1] = parent;
child = tempstore;
}
}
for(i = 0; i < child_size; i++)
printf("%d\n", child);
return (0);
}
Another alternative is to walk the entire array and count the number
of child entries. Then allocate memory for the child array using the
number of child entries, again run through parent array , check for
the condition and put the element (which satisfies the condition) in
the child array. This requires two passes to be made on an array. What
I posted is just a sample code. In my actual project, there can be as
many as 10,000,000 entries in the parent array. Do you think going
through 10,000,000 entries twice is a better idea than using realloc ?