C指针,指向二维数组 [英] C pointer to two dimensional array

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

问题描述

我知道有关于它提供了良好的(和工作)解决方案的几个问题,但恕我直言没有这显然说的话是实现这一目标的最佳途径。
因此,假设我们有一些二维数组:

  INT TAB1 [100] [280];

我们希望使指向这个二维数组的指针。
要做到这一点,我们可以这样做:

  INT(*指针)[280]; //创建的指针
指针= TAB1; //分配
指针[5] [12] = 517; // 使用
INT敏=指针[5] [12]; // 使用

,或者:

  INT(*指针)[100] [280]; //创建的指针
指针=安培; TAB1; //分配
(*指针)[5] [12] = 517; // 使用
INT敏=(*指针)[5] [12]; // 使用

OK,都似乎运作良好。现在,我想知道:


  • 什么是最好的方式,第一或第二?

  • 都等于该编译器? (速度,香水......)

  • 是这些解决方案吃更多的内存比其他的吗?

  • 什么是更经常使用的开发?


解决方案

  //定义的2​​80指针(1120或2240个字节)的数组
为int *指针1 [280];//定义一个指针(取决于32/64位平台4或8个字节)
INT(*指针2)[280]; //指向280整数数组
INT(* pointer3)[100] [280]; //指向100 * 280的整数二维数组

使用指针2 pointer3 产生除操作为 ++指针2 <相同的二进制/ code>由 WhozCraig

我推荐使用的typedef (产生相同的二进制code如上 pointer3

 的typedef INT的myType [100] [280];
*的myType pointer3;

注意:由于C ++ 11,你还可以使用关键字使用而不是的typedef

 使用的myType = INT [100] [280];
*的myType pointer3;

在你的例子:

  *的myType指针; //创建的指针
指针=安培; TAB1; //分配
(*指针)[5] [12] = 517; //集(写)
INT敏=(*指针)[5] [12]; //获取(读)

注意:如果数组 TAB1 是函数体=>这个数组将被放置在调用堆栈存储器内内使用。但堆栈大小是有限的。使用数组比可用内存堆越大产生堆栈溢出的崩溃

全片段是在线编译在 gcc.godbolt.org

  INT的main()
{
    //定义的2​​80指针(1120或2240个字节)的数组
    为int *指针1 [280];
    static_assert(的sizeof(指针1)== 2240,);    //定义一个指针(取决于32/64位平台4或8个字节)
    INT(*指针2)[280]; //指向280整数数组
    INT(* pointer3)[100] [280]; //指向100 * 280的整数二维数组
    static_assert(的sizeof(指针2)== 8,);
    static_assert(的sizeof(pointer3)== 8,);    //使用'的typedef(或使用'如果你使用了现代的C ++编译器)
    的typedef INT的myType [100] [280];
    //使用的myType = INT [100] [280];    INT TAB1 [100] [280];    *的myType指针; //创建的指针
    指针=安培; TAB1; //分配
    (*指针)[5] [12] = 517; //集(写)
    INT敏=(*指针)[5] [12]; //获取(读)    返回敏;
}

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

or, alternatively :

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 :

  • what is the best way, the 1st or the 2nd ?
  • are both equals for the compiler ? (speed, perf...)
  • is one of these solutions eating more memory than the other ?
  • what is the more frequently used by developers ?

解决方案

//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

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

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

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

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

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

in your example:

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

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.

The full snippet is online-compilable at 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指针,指向二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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