C Programming: Dynamic allocation of 2D arrays using an array of pointers.

In summary, the conversation discusses the creation of a dynamic 2D array in C programming. The code for this is provided and explained, with a specific focus on the function of the statement "sizeof(int *)". It is determined that this statement allocates an array of pointers and stores the pointer to the array in matA. The conversation also covers the importance of freeing the allocated memory after use.
  • #1
ashwinnarayan
18
0

Homework Statement


Ok, I'm learning C programming and I'm trying to create a dynamic 2D array. This is the code that seems to work in creating a dyamic array. I found it in a book.

Code:
    //rows and cols are values entered by the user when the program runs.
    int **(matA) = malloc(rows*sizeof(int *));
    
    matA[0] = malloc(rows*cols*sizeof(int));

    for(i=1;i<rows;i++) matA[i] = matA[0] + (i*cols);

My question is: What is the function of the statement sizeof(int *)?

I ran the program with a printf statement to print the value of sizeof(int *) and it came up as 8. I don't understand why this first malloc statement even exists. If I delete the first malloc statement then the code doesn't run.
 
Last edited:
Physics news on Phys.org
  • #2
Code:
    int **(matA) = malloc(rows*sizeof(int *));
This statement allocates an array of (rows) pointers and stores the pointer to the array of pointers in matA. Apparently you're running in 64-bit mode since the size of a pointer is 8 bytes.

Code:
    matA[0] = malloc(rows*cols*sizeof(int));
This statement allocates a single block of (rows * cols) integers for all of the matrix and stores it in matA[0], the pointer to the first row. The loop stores the pointers for the remaining rows. When your code is done it should free the memory as follows:

Code:
    free(matA[0]);  /* free block of integers */
    free(matA);     /* free array of pointers */
 
Last edited:
  • #3
Oh! I get it now. I was confused by the two layers of pointers. The initial statement allocates memory for the array of pointers and since the array needs to hold pointers each element needs to have enough memory to store pointers of type (int *).

Thanks!
 

Related to C Programming: Dynamic allocation of 2D arrays using an array of pointers.

What is dynamic allocation in C programming?

Dynamic allocation in C programming refers to the process of allocating memory for variables or data structures at run-time, rather than at compile-time. This allows for more flexible and efficient use of memory, as the exact size of the variable or data structure may not be known until the program is running.

What is a 2D array in C programming?

A 2D array in C programming is a data structure that stores data in a grid or matrix format, with rows and columns. It is essentially a one-dimensional array of one-dimensional arrays, allowing for the storage of multiple elements of the same data type in a structured manner.

What is an array of pointers in C programming?

An array of pointers in C programming is an array that stores memory addresses rather than actual data values. This allows for more efficient use of memory, as it allows for the manipulation and access of data at specific memory locations.

How do you dynamically allocate a 2D array using an array of pointers in C programming?

To dynamically allocate a 2D array using an array of pointers in C programming, you would first allocate memory for the array of pointers using the malloc() function. Then, you would use a loop to allocate memory for each row of the 2D array and assign the corresponding memory address to the array of pointers. Finally, you can use the 2D array like a regular array, accessing and manipulating data as needed.

What are the advantages of using dynamic allocation for 2D arrays in C programming?

Using dynamic allocation for 2D arrays in C programming allows for a more flexible and efficient use of memory. It also allows for the creation of 2D arrays with varying sizes at run-time, making the program more adaptable. Additionally, dynamically allocated 2D arrays can be easily resized and deallocated, reducing the risk of memory leaks and improving overall program performance.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
771
  • Engineering and Comp Sci Homework Help
Replies
4
Views
960
  • Engineering and Comp Sci Homework Help
Replies
3
Views
690
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
Back
Top