Dynamically allocating array explain

Richard

This is sample code my teacher showed us about "How to dynamically allocate an array in C?". But I don't fully understand this. Here is the code:

int k;
int** test;
printf("Enter a value for k: ");
scanf("%d", &k);
test = (int **)malloc(k * sizeof(int*));
for (i = 0; i < k; i++) {
    test[i] = (int*)malloc(k * sizeof(int)); //Initialize all the values
}

I thought in C, to define an array you had to put the [] after the name, so what exactly is int** test; isn't it just a pointer to a pointer? And the malloc() line is also really confusing me.....

Grijesh Chauhan

According to declaration int** test; , test is pointer to pointer, and the code pice allocating memory for a matrix of int values dynamically using malloc function.

Statement:

test = (int **)malloc(k * sizeof(int*));
    //                ^^------^^-------
    //  allocate for  k  int*  values    

Allocate continue memory for k pointers to int (int*). So suppose if k = 4 then you gets something like:

 temp      343  347  351  355
+----+    +----+----+----+----+
|343 |---►| ?  | ?  | ?  |  ? |
+----+    +----+----+----+----+

I am assuming addresses are of four bytes and ? means garbage values.

temp variable assigned returned address by malloc, malloc allocates continues memory blocks of size = k * sizeof(int**) that is in my example = 16 bytes.

In the for loop you allocate memory for k int and assign returned address to temp[i] (location of previously allocated array).

test[i] = (int*)malloc(k * sizeof(int)); //Initialize all the values
//                     ^^-----^^----------
//       allocate for  k   int  values    

Note: the expression temp[i] == *(temp + i). So in for loop in each iterations you allocate memory for an array of k int values that looks something like below:

   First malloc                     For loop   
  ---------------                  ------------------
       temp
      +-----+
      | 343 |--+
      +-----+  |
               ▼                    201   205   209    213  
        +--------+                +-----+-----+-----+-----+
 343    |        |= *(temp + 0)   |  ?  |  ?  |  ?  | ?   |  //for i = 0
        |temp[0] |-------|        +-----+-----+-----+-----+
        | 201    |       +-----------▲
        +--------+                  502   506  510    514
        |        |                +-----+-----+-----+-----+
 347    |temp[1] |= *(temp + 1)   |  ?  |  ?  |  ?  | ?   |  //for i = 1
        | 502    |-------|        +-----+-----+-----+-----+
        +--------+       +-----------▲
        |        |                  43    48    52    56
 351    | 43     |                +-----+-----+-----+-----+
        |temp[2] |= *(temp + 2)   |  ?  |  ?  |  ?  | ?   |  //for i = 2
        |        |-------|        +-----+-----+-----+-----+
        +--------+       +-----------▲
 355    |        |
        | 9002   |                 9002  9006   9010 9014
        |temp[3] |                +-----+-----+-----+-----+
        |        |= *(temp + 3)   |  ?  |  ?  |  ?  | ?   |  //for i = 3
        +--------+       |        +-----+-----+-----+-----+
                         +-----------▲

Again ? means garbage values.

Additional points:

1) You are casting returned address by malloc but in C you should avoid it. Read Do I cast the result of malloc? just do as follows:

test = malloc(k* sizeof(int*));
for (i = 0; i < k; i++){
    test[i] = malloc(k * sizeof(int));
}

2) If you are allocating memory dynamically, you need to free memory explicitly when your work done with that (after freeing dynamically allocated memory you can't access that memory). Steps to free memory for test will be as follows:

for (i = 0; i < k; i++){
    free(test[i]);
}
free(test);

3) This is one way to allocate memory for 2D matrix as array of arrays if you wants to allocate completely continues memory for all arrays check this answer: Allocate memory 2d array in function C

4) If the description helps and you want to learn for 3D allocation Check this answer: Matrix of String or/ 3D char array

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Dynamically Allocating Array With Datafile

From Dev

Dynamically allocating an array of a struct

From Dev

Dynamically allocating an array in a function in C

From Dev

Dynamically allocating a 2D array in C

From Dev

Dynamically allocating a 2D string array

From Dev

C - Dynamically allocating array for stdin without realloc

From Dev

Dynamically allocating array of struct from file C

From Dev

dynamically allocating an int array inside a structure

From Dev

Trouble dynamically allocating memory for string array

From Dev

Allocating memory for array of pointers of int dynamically

From Dev

Allocating 2D array dynamically

From Dev

c: issues when allocating 2d char array dynamically?

From Dev

Reading file data to array of structures while allocating memory dynamically

From Dev

C segmentation fault when dynamically allocating 2d array

From Dev

Forward Declaring and Dynamically Allocating an Array of Pointers of that Declared Class?

From Dev

Reading file data to array of structures while allocating memory dynamically

From Dev

dynamically allocating my 2d array in c

From Dev

Dynamically allocating 1D array of structs : Two Methods

From Dev

Dynamically Allocating String Arrays

From Dev

Dynamically allocating threads in C

From Dev

Dynamically allocating lines from stdin?

From Dev

Dynamically allocating struct inside a loop

From Dev

dynamically allocating port in Node JS

From Dev

recv() on socket by dynamically allocating space

From Dev

Dynamically allocating memory for changing array size starting with unknown size C++

From Dev

When dynamically allocating memory for a list of strings that will be put in an array, is it possible to add on to that list later in the program?

From Dev

C Programming: Reading data from a file, dynamically allocating memory, placing contents in struct array

From Dev

when allocating memory for an array dynamically (in C), what does the (int *) cast do?

From Dev

Allocating array of structures with malloc

Related Related

  1. 1

    Dynamically Allocating Array With Datafile

  2. 2

    Dynamically allocating an array of a struct

  3. 3

    Dynamically allocating an array in a function in C

  4. 4

    Dynamically allocating a 2D array in C

  5. 5

    Dynamically allocating a 2D string array

  6. 6

    C - Dynamically allocating array for stdin without realloc

  7. 7

    Dynamically allocating array of struct from file C

  8. 8

    dynamically allocating an int array inside a structure

  9. 9

    Trouble dynamically allocating memory for string array

  10. 10

    Allocating memory for array of pointers of int dynamically

  11. 11

    Allocating 2D array dynamically

  12. 12

    c: issues when allocating 2d char array dynamically?

  13. 13

    Reading file data to array of structures while allocating memory dynamically

  14. 14

    C segmentation fault when dynamically allocating 2d array

  15. 15

    Forward Declaring and Dynamically Allocating an Array of Pointers of that Declared Class?

  16. 16

    Reading file data to array of structures while allocating memory dynamically

  17. 17

    dynamically allocating my 2d array in c

  18. 18

    Dynamically allocating 1D array of structs : Two Methods

  19. 19

    Dynamically Allocating String Arrays

  20. 20

    Dynamically allocating threads in C

  21. 21

    Dynamically allocating lines from stdin?

  22. 22

    Dynamically allocating struct inside a loop

  23. 23

    dynamically allocating port in Node JS

  24. 24

    recv() on socket by dynamically allocating space

  25. 25

    Dynamically allocating memory for changing array size starting with unknown size C++

  26. 26

    When dynamically allocating memory for a list of strings that will be put in an array, is it possible to add on to that list later in the program?

  27. 27

    C Programming: Reading data from a file, dynamically allocating memory, placing contents in struct array

  28. 28

    when allocating memory for an array dynamically (in C), what does the (int *) cast do?

  29. 29

    Allocating array of structures with malloc

HotTag

Archive