Thread: Question on matrix initialization

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    13

    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?

    Thank's in advance!

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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]?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    13
    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. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    13
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Matrix Question
    By CJM in forum C Programming
    Replies: 6
    Last Post: 12-03-2010, 12:18 AM
  2. String Declaration and Initialization Question
    By limblet2 in forum C Programming
    Replies: 3
    Last Post: 03-06-2009, 01:46 AM
  3. matrix question
    By ronenk in forum C Programming
    Replies: 11
    Last Post: 08-04-2004, 07:19 PM
  4. Matrix question
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 03-30-2003, 05:35 PM
  5. Initialization question
    By cpp4ever in forum C++ Programming
    Replies: 0
    Last Post: 10-19-2001, 05:09 AM