Thread: dynamic 2D array handling ?

  1. #1
    Registered User (*)BLUE's Avatar
    Join Date
    Aug 2002
    Posts
    6

    Question dynamic 2D array handling ?

    I am very new to the programming. Please don't be too harsh on me.^^

    I allocate two dimensional array, get an input and want to printf the data.
    Could you tell me what may be wrong with this code, other than not freeing memory?

    The code compiled ok. Two scanfs work fine.
    Error occurs at the last printf on main, when I print out the math data.
    The error message says "access violation", which I don't understand why.
    BTW, I am using VC6.0.

    Code:
    #include<stdio.h> 
    #include<stdlib.h> 
    
    void getdata(int **a){ 
        int num_data=0,i; 
        printf("Input the number : "); 
        scanf("%d",&num_data); 
    
        /* allocacate two D. */
        a=(int **)malloc(num_data*sizeof(int*)); 
        for(i=0 ; i<num_data ; i++){ 
            a[i]=(int*)malloc(3*sizeof(int)); 
        } 
    
        printf("Math: "); 
        scanf("%d",&a[0][0]); 
    
        /* I know I have to delocate the memory somewhere. */
    } 
    
    int main(){ 
        int **a; 
        getdata(a); 
        printf("%d\n",a[0][0]); /* error */
        return 0;
    }

  2. #2
    Registered User (*)BLUE's Avatar
    Join Date
    Aug 2002
    Posts
    6

    Lightbulb Oh.....

    Oh.... I see.

    So if I want to use &a,
    I shoud have used
    void getdata(int *** a){/*...*/}

    Am I right?

  3. #3
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Also you're better of allocating your entire array in one block rather than as an array of pointers. So, for example, for a 10 x 3 Array of ints you'd be better off doing:

    int Array[30]; // 10 x 3

    // And then access elements by

    Array[ RowToAccess * NumCols + ColToAccess ];

    This is actually how multidimensional arrays are stored and accessed internally by the compiler when you don't dynamically create one. It shortens your construction time, shortens your destruction time, takes up less memory, and prevents memory fragmentation. And if you really don't like accessing like that, you can always put it in a function.

  4. #4
    Registered User (*)BLUE's Avatar
    Join Date
    Aug 2002
    Posts
    6
    Thank you all.

    I will keep that in mind.

  5. #5
    Registered User (*)BLUE's Avatar
    Join Date
    Aug 2002
    Posts
    6
    Later on, watch for precedence
    (*a)[i]=malloc(3*sizeof(int));
    Oh, I could've make that error too.
    Thank you Salem.

  6. #6
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Salem
    int (*arr)[3] = malloc ( x * sizeof(*arr) );
    Very true, however you can use that if and only if you know the value of the last one or more dimensions of the array you are allocating at compile time which isn't always the case. It's good to get a full understanding of array representation and access for more varying circumstances.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. handling 2D array..
    By transgalactic2 in forum C Programming
    Replies: 14
    Last Post: 03-29-2009, 07:48 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Allocating a 2D Array Differently
    By pianorain in forum C++ Programming
    Replies: 13
    Last Post: 12-15-2005, 02:01 AM
  4. Dynamic 2d array
    By Mithoric in forum C++ Programming
    Replies: 8
    Last Post: 12-29-2003, 09:19 AM
  5. 2D dynamic array problem
    By scsullivan in forum C Programming
    Replies: 3
    Last Post: 12-30-2002, 10:02 PM