I am currently learning C and have to program a "dynamic array".
In a header file provided to us, the struct DynArray is declared as
struct DynamicArray
{
unsigned int size;
unsigned int capacity;
int *data;
};
I have already implemented most of the functions in the dyn_array program, which was provided with empty functions.
My difficutly lies with the function dn_append(DynamicArray *a, int elem). The only description I have been given is
// =====================================================================================
// Name: dn_append
// Description: Append an element.
//
// Parameters: a - the array
// elem - the new value
// Returns: non-zero, if the array was successfully extended
// =====================================================================================
We have a makefile to compile this and a few test cases. In one of the test programs, a new DynArray is initialized and then a few values are appended:
int
main ( int argc, char *argv[] )
{
DynamicArray *a1;
int i;
a1 = dn_new ( 5 );
dn_append ( a1, 5 );
dn_append ( a1, 7 );
dn_append ( a1, 8 );
dn_append ( a1, 11 );
dn_append ( a1, 13 );
dn_append ( a1, 15 );
dn_set ( a1, 2, 9 );
for ( i = 0; i < dn_size ( a1 ); i += 1 )
{
printf ( "%d\n", dn_get ( a1, i ) );
}
dn_destory ( a1 );
return 0;
}
It aborts with a segmentation fault.
My (faulty) implementation is as follows. The outer else-case is completely messed up, since the debugging drove me crazy. (Note that I explain the problematic line after the code sample.)
int
dn_append ( DynamicArray *a, int elem )
{
printf("\n\nAppend:\n");
if (a->size >= a->capacity) {
printf("Array too small");
int *dataPtr = realloc(a->data, 2*a->capacity);
if (dataPtr != NULL) {
a->capacity *= 2;
a->data = dataPtr;
a->data[a->size] = elem;
a->size++;
}
else {
return 0;
}
}
else {
int *dataPtr;
dataPtr = a->data;
printf("Size: %d, Capacity: %d\n", a->size, a->capacity);
int sizeN = a->size;
printf("Size: %d, Capacity: %d\n", a->size, a->capacity);
//int offset = sizeN;
int *temp;
temp = dataPtr;// + offset;
//dataPtr[offset] = elem;
//printf("Data at %d is %d, should be %d\n", offset, *(a->data), elem);
a->size++;
}
return 1;
}
The problematic line is in the outer else-case, in the middle:
printf("Size: %d, Capacity: %d\n", a->size, a->capacity);
int sizeN = a->size;
printf("Size: %d, Capacity: %d\n", a->size, a->capacity);
When I run the program, these lines print out
Size: 0, Capacity: 5
Size: 0, Capacity: 0
I don't even touch the capacity
-component of the struct, but it sets it to 0, which completely f***s up the following program.
After commenting the line int sizeN = a->size;
, the capacity
is left right as it should.
I need to read the size
, one way or another.
So, why the hell does it change that component?
Some additional infos:
DynamicArray*
dn_new ( unsigned int capacity )
{
if (capacity > 0) {
int *dataPtr = malloc(capacity*sizeof(int));
if (dataPtr != NULL) {
struct DynamicArray array = { 0, capacity, dataPtr };
return &array;
}
else {
return NULL;
}
}
else {
return NULL;
}
}