# Thread: Question on matrix initialization

1. ## Question on matrix initialization

Good morning everybody!
I'm working on an application where I have a structure and I'll use its parameters in a bidimensional matrix [n][k], where n is known and k is unknown and might be different for each index of the dimension n. I've fixed a "kick" for k, and I use realloc to increase memory as k increases.

I need to store the value of k in the structure, so I thought about putting the value of each k in the first index of the second dimension:

Code:
`array[i][0].n = k_current;`
so I know the number of elements in the second dimension for each i, what will help me when I'll read the values of the structure.

My explanation might be confusing, so here is a piece of code I've made to illustrate it better (that's just an example, the matrix will not be filled this way, and I've fixed n=2):

(Type values for each k and -1 to finish).

Code:
```typedef struct {
double a;
int b;
} FOO;

int main()
{
int i, j, k;
double x = 0;
FOO **arr;
int n = 2;
int kick = 2;
arr = malloc(n*sizeof(FOO*));

for(i=0; i<n; i++)
{
x = 0;
k = 0;
printf("Type arr[%d][k].a: \n", i);
while(x != -1)
{
if(k == 0) arr[i] = malloc(kick*sizeof(FOO));
printf("k = %d: ", k);
scanf("%lf", &x);
if(x == -1) break; // that's just an example
if(k == kick)
{
kick += 2;
FOO *temp = realloc(arr[i], kick*sizeof(FOO));
if(!temp) exit(1);
arr[i] = temp;
}
arr[i][k++].a = x;
}
arr[i][0].b = k; // Here's my question!
printf("Ok, you've typed %d values of a.\n", arr[i][0].b);
}
printf("\nEND.");
getch();
}```
Do you think this is a good way to store the values of k? I'm worried because the parameter b won't be defined for the other indexes of the second dimension. Is that a problem, even if I won't access them? And also, should I initialize the other parameters of the structure in the array?

2. Short answer: I do not consider that it is a good idea to store the size of an array within members of that array. Particularly as you are only placing the size within a member (b) of the first element, meaning that your struct wastes space unnecessarily for all other elements.

Given that n is known, why not maintain a second dynamically allocated array of n size_t's, so that size[i] stores the size of arr[i]?

3. Thank you, it's really better this way, using another array.
And should I initialize the parameters ('a' in this case) before I start to attribute values? Even if I use realloc there will be some indexes where 'a' won't be defined (I won't access them, though).

4. If you can be 100% sure your code will never retrieve the value of an uninitialised 'a' then fine. If you are "sort of" sure (i.e. some value less than 100% certain) then initialise the values somehow before any code that (attempts to) retrieve a value.

If realloc() is used to give a size increase, then the additional structs in the array will be uninitialised. Guaranteed. Accessing members of those structs will have undefined behaviour.

5. Ok, with what I've written until now I can be sure I won't access an uninitialized 'a'. In fact I count the number of elements inserted in each second index to avoid this.

Thank's again.