中二维数组的指针

中二维数组的指针

本文介绍了指向 C 中二维数组的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有几个关于提供良好(和有效)解决方案的问题,但恕我直言,没有一个明确说明实现这一目标的最佳方法是什么.所以,假设我们有一些二维数组:

I know there is several questions about that which gives good (and working) solutions, but none IMHO which says clearly what is the best way to achieve this.So, suppose we have some 2D array :

int tab1[100][280];

我们想要创建一个指向这个二维数组的指针.为此,我们可以这样做:

We want to make a pointer that points to this 2D array.To achieve this, we can do :

int (*pointer)[280]; // pointer creation
pointer = tab1; //assignation
pointer[5][12] = 517; // use
int myint = pointer[5][12]; // use

或者,或者:

int (*pointer)[100][280]; // pointer creation
pointer = &tab1; //assignation
(*pointer)[5][12] = 517; // use
int myint = (*pointer)[5][12]; // use

好的,两者似乎都运行良好.现在我想知道:

OK, both seems to work well. Now I would like to know :

  • 最好的方法是第一种还是第二种?
  • 对于编译器来说两者是否相等?(速度,性能...)
  • 这些解决方案之一是否比其他解决方案消耗更多内存?
  • 开发人员最常用的是什么?

推荐答案

//defines an array of 280 pointers (1120 or 2240 bytes)
int  *pointer1 [280];

//defines a pointer (4 or 8 bytes depending on 32/64 bits platform)
int (*pointer2)[280];      //pointer to an array of 280 integers
int (*pointer3)[100][280]; //pointer to an 2D array of 100*280 integers

使用 pointer2pointer3 产生相同的二进制文件,除了 WhozCraig.

Using pointer2 or pointer3 produce the same binary except manipulations as ++pointer2 as pointed out by WhozCraig.

我推荐使用 typedef(产生与上述 pointer3 相同的二进制代码)

I recommend using typedef (producing same binary code as above pointer3)

typedef int myType[100][280];
myType *pointer3;

注意:从C++11开始,你也可以使用关键字using代替typedef

Note: Since C++11, you can also use keyword using instead of typedef

using myType = int[100][280];
myType *pointer3;

在您的示例中:

myType *pointer;                // pointer creation
pointer = &tab1;                // assignation
(*pointer)[5][12] = 517;        // set (write)
int myint = (*pointer)[5][12];  // get (read)

注意:如果在函数体中使用数组 tab1 => 该数组将被放置在调用堆栈内存中.但是堆栈大小是有限的.使用大于可用内存堆栈的数组会导致堆栈溢出崩溃.

Note: If the array tab1 is used within a function body => this array will be placed within the call stack memory. But the stack size is limited. Using arrays bigger than the free memory stack produces a stack overflow crash.

完整代码段可在 gcc.godbolt.org

int main()
{
    //defines an array of 280 pointers (1120 or 2240 bytes)
    int  *pointer1 [280];
    static_assert( sizeof(pointer1) == 2240, "" );

    //defines a pointer (4 or 8 bytes depending on 32/64 bits platform)
    int (*pointer2)[280];      //pointer to an array of 280 integers
    int (*pointer3)[100][280]; //pointer to an 2D array of 100*280 integers
    static_assert( sizeof(pointer2) == 8, "" );
    static_assert( sizeof(pointer3) == 8, "" );

    // Use 'typedef' (or 'using' if you use a modern C++ compiler)
    typedef int myType[100][280];
    //using myType = int[100][280];

    int tab1[100][280];

    myType *pointer;                // pointer creation
    pointer = &tab1;                // assignation
    (*pointer)[5][12] = 517;        // set (write)
    int myint = (*pointer)[5][12];  // get (read)

    return myint;
}

这篇关于指向 C 中二维数组的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 17:19