0

I am trying to dynamically allocate memory to a 2-D array through creating a pointer to a pointer. In other words, I am trying to emulate the following C code:

#include <stdio.h> 
#include <stdlib.h> 
  
int main() 
{ 
    int r = 3, c = 3, i, j, count; 
  
    int **arr = (int **)malloc(r * sizeof(int *)); 
    for (i=0; i<r; i++) 
         arr[i] = (int *)malloc(c * sizeof(int)); 
}

I was breaking down this problem into two problems:

  1. Allocating memory for the entire matrix:

int **arr = (int **)malloc(r * sizeof(int *));

  1. Allocating memory for each row in the matrix:
for (i=0; i<r; i++) 
     arr[i] = (int *)malloc(c * sizeof(int));

I think I can finish the first part, because I can dynamically allocate a section of memory through calling the C malloc function in my Assembly code (for example, 12 bytes here, since in the C example I am using, the matrix has 3 rows and an integer pointer (int*) has 4 bytes of space):

movl $12, %edi
extern malloc
call malloc

however at the second part, how would I go about allocating memoery at each row? The code above is going to return a pointer to the matrix in the EAX register, so I was thinking about:

movl ($eax), %eax

Which would return the memory address of the first row. How do I then allocate space at that specific memory address? From my basic knowledge of Assembly so far, malloc can only allocate memory and then return a pointer to a memory address, but I'm not sure how to use that command to allocate memory at a pointer to a specific memory address.

Adam Lee
  • 436
  • 1
  • 14
  • 49
  • 1
    That's not a 2D array, that's an array of pointers to arrays. In C, a 2D array is a real thing (`int foo[rows][cols]`, an array of arrays) where *all* the `int` elements are contiguous with each other, no pointers involved. You allocate one by calling `malloc(sizeof(int) * rows * cols)`. (And assign the result to `int (*arr2d)[cols]`, [Malloc a 2D array in C](https://stackoverflow.com/q/36890624)) – Peter Cordes Nov 21 '20 at 10:18
  • @PeterCordes Thank you for the quick response :D. Since you said that the above example is an array of pointers to arrays, that means that the pointers to the row arrays are contiguously allocated. So would the solution be to have a loop, allocating the row arrays with malloc(cols * 4), and since malloc returns a pointer to that array, pushing that pointer to the proper spot in the array of row pointers before finally pushing the pointer to the first row pointer? – Adam Lee Nov 21 '20 at 10:47
  • 1
    Why do you want an array of pointers in the first place, instead of an actual 2D array? In C, the normal excuse for this inefficiency is that the syntax for using it can be easier, but that doesn't apply in asm. In asm, indexing via `row * row_width + col * element_width` is usually easier than dereferencing twice. But yes, if you insist on doing it the dumb way, you call `malloc` N times, storing the results into elements of the initial array-of-pointers. – Peter Cordes Nov 21 '20 at 10:55
  • @PeterCordes Gotcha. I think I'll switch to indexing instead :D. – Adam Lee Nov 21 '20 at 11:12

0 Answers0