如何多维数组在内存格式化? [英] How are multi-dimensional arrays formatted in memory?

查看:153
本文介绍了如何多维数组在内存格式化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C,我知道我可以用下面的code动态地分配在堆上一个二维数组:

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*));

for (i = 0; i < arrayRows; i++) {
    someNumbers[i] = malloc(arrayColumns*sizeof(int));
}

显然,这实际上创建指针的一维数组一堆整数独立的一维数组,而系统可以找出我的意思是,当我问:

Clearly, this actually creates a one-dimensional array of pointers to a bunch of separate one-dimensional arrays of integers, and "The System" can figure out what I mean when I ask for:

someNumbers[4][2];

但是,当我静声明一个二维数组,如下面的行...

But when I statically declare a 2D array, as in the following line...:

int someNumbers[ARRAY_ROWS][ARRAY_COLUMNS];

...没有一个类似的结构得到在栈上创建的,或者是另一种形式的彻底? (即是它的指针一维数组?如果不是,它是什么,以及如何引用它得到想通了?)

...does a similar structure get created on the stack, or is it of another form completely? (i.e. is it a 1D array of pointers? If not, what is it, and how do references to it get figured out?)

此外,当我说,系统,究竟是怎样负责盘算,出来?内核?还是C编译器把它清除出来,而编译?

Also, when I said, "The System," what is actually responsible for figuring that out? The kernel? Or does the C compiler sort it out while compiling?

推荐答案

一个静态二维数组看起来像一个数组的数组 - 它只是在内存布局连续。数组是不一样的东西作为指针,而是因为你经常可以互换多使用它们pretty它可以变得混乱,有时。编译器跟踪正常,虽然,这使得一切排队很好。你必须要小心静态二维数组就像你提到的,因为如果你试图通过一个函数服用 INT ** 参数,不好的事情将要发生。这里有一个简单的例子:

A static two-dimensional array looks like an array of arrays - it's just laid out contiguously in memory. Arrays are not the same thing as pointers, but because you can often use them pretty much interchangeably it can get confusing sometimes. The compiler keeps track properly, though, which makes everything line up nicely. You do have to be careful with static 2D arrays like you mention, since if you try to pass one to a function taking an int ** parameter, bad things are going to happen. Here's a quick example:

int array1[3][2] = {{0, 1}, {2, 3}, {4, 5}};

在内存看起来是这样的:

In memory looks like this:

0 1 2 3 4 5

究竟一样的:

int array2[6] = { 0, 1, 2, 3, 4, 5 };

但是,如果你尝试通过数组1 来此功能:

void function1(int **a);

你会得到一个警告(和应用程序将无法正确访问数组):

you'll get a warning (and the app will fail to access the array correctly):

warning: passing argument 1 of ‘function1’ from incompatible pointer type

由于二维数组是不一样的 INT ** 。数组的自动衰变成一个指针唯一无二深一平可以这么说。您需要将函数声明为:

Because a 2D array is not the same as int **. The automatic decaying of an array into a pointer only goes "one level deep" so to speak. You need to declare the function as:

void function2(int a[][2]);

void function2(int a[3][2]);

为使一切快乐。

此相同的概念延伸到的 N 的维数组。趁着这种滑稽的生意在你的应用程序一般只就更难理解,虽然。所以,要小心在那里。

This same concept extends to n-dimensional arrays. Taking advantage of this kind of funny business in your application generally only makes it harder to understand, though. So be careful out there.

这篇关于如何多维数组在内存格式化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆