How are multi-dimensional arrays formatted in memory?

前端 未结 6 1395
名媛妹妹
名媛妹妹 2020-11-22 03:51

In C, I know I can dynamically allocate a two-dimensional array on the heap using the following code:

int** someNumbers = malloc(arrayRows*sizeof(int*));

fo         


        
6条回答
  •  不要未来只要你来
    2020-11-22 04:58

    In answer to your also: Both, though the compiler is doing most of the heavy lifting.

    In the case of statically allocated arrays, "The System" will be the compiler. It will reserve the memory like it would for any stack variable.

    In the case of the malloc'd array, "The System" will be the implementer of malloc (the kernel usually). All the compiler will allocate is the base pointer.

    The compiler is always going to handle the type as what they are declared to be except in the example Carl gave where it can figure out interchangeable usage. This is why if you pass in a [][] to a function it must assume that it is a statically allocated flat, where ** is assumed to be pointer to pointer.

提交回复
热议问题