Vladislav Martin - 1 year ago 110

C Question

I have read other questions that are somewhat related to this topic on StackOverflow (namely (1) and (2)), but they do not address my question regarding a member array specified within a struct in a manner that I could follow logically.

In C, I specify a double pointer as a member of my struct to store a 2-D dynamic array.

`typedef struct CharArrayXY`

{

char **array; // A look-up table pointing to a 2-D arrangement of values stored in memory

size_t used_x; // Number of arrays occupying 2-D array

size_t *used_y; // Number of elements occupying each array

size_t size_x; // Number of spaces allocated for arrays in 2-D array

size_t *size_y; // Number of spaces allocated for elements in each array

} CharArrayXY;

I initialize all of the members of the struct with a

`malloc()`

`a->array = malloc(sizeof(*a->array) * x_dim);`

// Check if it's NULL...

for (x_ind = 0; x_ind < x_dim; x_ind++)

{

a->array[x_ind] = malloc(sizeof(char) * y_dim);

}

a->size_x = x_dim;

a->size_y = malloc(sizeof(size_t) * x_dim);

// Check if it's NULL...

for (x_ind = 0; x_ind < x_dim; x_ind++)

{

a->size_y[x_ind] = y_dim;

}

a->used_x = 0;

a->used_y = malloc(sizeof(size_t) * x_dim);

// Check if it's NULL...

for (x_ind = 0; x_ind < x_dim; x_ind++)

{

a->used_y[x_ind] = 0;

}

I attempt to resize the array with

`realloc()`

`void add_char_xy (CharArrayXY *a, char element, size_t x_ind)`

{

if (a->used_x == a->size_x)

{

printf("Reallocating for more space...\n");

a->size_x += 2;

temp_ptr = realloc(a->array, sizeof(*a->array) * a->size_x);

if (temp_ptr == NULL)

{

fprintf(stderr, "realloc() call failed.\n");

return;

}

a->array = temp_ptr;

}

if (a->used_y[x_ind] == a->size_y[x_ind])

{

a->size_y[x_ind] += 10;

a->array[x_ind] = realloc(a->array[x_ind], sizeof(*a->array[x_ind]) * a->size_y[x_ind]);

}

printf("Storing '%c' in CharArrayXY[%zu][%zu].\n", element, x_ind, a->used_y[x_ind]);

a->array[x_ind][a->used_y[x_ind]] = element;

a->used_y[x_ind]++;

}

Then, everything is called.

`CharArrayXY two_array;`

size_t x_dim = 5;

size_t y_dim = 10;

init_xy(&two_array, x_dim, y_dim);

// Assign random characters to my array

char random_letter;

size_t x_ind, y_ind;

for (x_ind = 0; x_ind < (x_dim + 3); x_ind++)

{

for (y_ind = 0; y_ind < y_dim; y_ind++)

{

random_letter = 'a' + (random() % 26);

add_char_xy(&two_array, random_letter, x_ind);

}

two_array.used_x++;

}

When I do this,

`used_y`

`56`

`0`

`realloc()`

`array`

`... Storing many characters in the array`

Storing 'b' in CharArrayXY[4][0]. <-- Array is being populated fine (hasn't been reallocated yet, still filling initialized array)

Storing 'l' in CharArrayXY[4][1].

Storing 'd' in CharArrayXY[4][2].

Storing 'b' in CharArrayXY[4][3].

Storing 'e' in CharArrayXY[4][4].

Storing 'f' in CharArrayXY[4][5].

Storing 's' in CharArrayXY[4][6].

Storing 'a' in CharArrayXY[4][7].

Storing 'r' in CharArrayXY[4][8].

Storing 'c' in CharArrayXY[4][9].

Reallocating for more space...

Storing 'b' in CharArrayXY[5][65]. <-- Right after the realloc() call

Segmentation fault (core dumped)

Why does it appear that the value of

`a->used_y[x_ind]`

`realloc()`

`array`

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

After some edits and comments :) the problem is:

You have to `realloc`

ate `a->used_y`

and all variables correlated to the size of `array`

each time you change the size (increase) of `array`

.

Make also sure to init new allocated memory of `a->used_y`

.

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**