I'm surprised it works that far. Count yourself lucky.
Code:
void growArray(struct blueprint *array)
{
struct blueprint *buffer;
int oldSize = size;
size *= 2;
buffer = (blueprint *)malloc(size * sizeof(struct blueprint));
for (int x = 0; x < oldSize; x++)
{
buffer[x] = array[x];
}
array = buffer;
}
This does not update the pointer outside of this function. You just think it does. You have two ways to fix this:
Code:
struct blueprint * growArray(struct blueprint *array)
{
...
return buffer;
}
Return it, and assign the return to 'array'. Or, use a pointer to a pointer:
Code:
void growArray(struct blueprint **array)
{
...
*array = buffer;
}
Of course, you have to update your other uses of 'array' in the function also.
Oh, by the way, you've got a huge memory leak every single time this function happens, because you never free the old contents of 'array'. Consider correctly using realloc instead of all of this. But even then you'll have to do what I mentioned before, you just can skip the free inside this function (which you're doing already, which is wrong... )
[edit]
And just to prove the point...
Code:
#include<stdio.h>
#include<stdlib.h>
struct a
{
int b;
};
void c( struct a * b )
{
struct a * d = malloc( sizeof( *d ) );
if( d )
d->b = 10;
b = d; /* doesn't work the way you think it does */
}
int main( void )
{
struct a * b = malloc( sizeof( *b ) );
if( b )
b->b = 5;
printf("b->b is %d\n", b->b );
c( b );
printf("b->b is %d\n", b->b );
printf("Oh, by the way, there's a memory"
" leak in C since 'd' is lost.\n");
return 0;
}
/*
gcc -o foo foo.c -Wall -pedantic -ansi
./foo
b->b is 5
b->b is 5
Oh, by the way, there's a memory leak in C since 'd' is lost.
*/
[/edit]
Quzah.