指针连续的二维数组 [英] Pointer to contiguous 2D array

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

问题描述

我使用gcc版本4.8.0与标志 -Wall -std = gnu99

我需要动态分配在C使用malloc一个连续的二维数组的存储;这个事实是不可谈判的。不过,为了便于使用,我还是希望能够访问使用便捷 X [R] [C] 标记的数组。这里是我创造一个指向一个连续的二维数组,并做索引数组勇敢的尝试 *数组[R] [C]

 的#include<&stdlib.h中GT;
#包括LT&;&stdio.h中GT;INT主要(无效)
{
    为size_t行数= 3,COLS = 5;    的printf(的sizeof(int)的=%李\\ n \\ n的sizeof(INT));    INT(*数组)[行] [COLS] =的malloc(sizeof的(INT)*行* COLS);
    的printf(数组始于%P \\ N,数组);
    的printf(的sizeof(数组)=%里\\ N的sizeof(阵列));
    的printf(的sizeof(数组[0] [0])=为0x%LX \\ N的sizeof(数组[0] [0]));
    卖出期权();    无符号短R,C;    为(R = 0;为r =行 - 1; R ++){
        为(C = 0; C< =列 - 1; C ++){
            的printf(数组[%I] [%i]为在%P \\ N,R,C,及(*数组[R] [C]));
        };
        卖出期权();
    };
}

虽然编译没有警告,它有一些意想不到的输出:

 的sizeof(INT)= 4阵列始于0x16cc010
的sizeof(数组)= 8
的sizeof(数组[0] [0])= 0x14的阵列[0] [0]是在0x16cc010
阵列[0] [1]是在0x16cc024
阵列[0] [2]是在0x16cc038
阵列[0] [3]是在0x16cc04c
阵列[0] [4]是在0x16cc060阵列[1] [0]是在0x16cc04c
阵列[1] [1]是在0x16cc060
阵列[1] [2]是在0x16cc074
阵列[1] [3]是在0x16cc088
阵列[1] [4]是在0x16cc09c阵列[2] [0]是在0x16cc088
阵列[2] [1]是在0x16cc09c
阵列[2] [2]是在0x16cc0b0
阵列[2] [3]是在0x16cc0c4
阵列[2] [4]是在0x16cc0d8

我真的不明白这里发生了什么。元素是连续的,但他们对于整数的方式过大,数组的方式太小,无法容纳所有的人。

我的数组声明是我最不确定部分 - 我读 INT(*数组)[行] [COLS] 阵列是一个指针阵列( COLS 步幅)的整数,但显然这是不正确。

我究竟做错了什么?


解决方案

  INT(*数组)[COLS] =的malloc(sizeof的*数组*行);为(R = 0;为r =行 -  1; R ++){
    为(C = 0; C< =列 - 1; C ++){
        的printf(数组[%I] [%i]为在%P \\ N,R,C,和放大器;数组[R] [C]);

这允许您指数阵列数组[I] [J] ,因为阵列[我] 隐式取消引用指针。

要释放,你只需要调用

 免费(数组);

I'm using gcc version 4.8.0 with flags -Wall -std=gnu99.

I need to dynamically allocate the memory for a contiguous 2D array using malloc in C; this fact is nonnegotiable. However, for ease of use I still want to be able to access the array using the convenient x[r][c] notation. Here's my valiant attempt at creating a pointer to a contiguous 2D array and indexing the array by doing *array[r][c]:

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
    size_t rows = 3, cols = 5;

    printf("sizeof(int) = %li\n\n", sizeof(int));

    int (*array)[rows][cols] = malloc(sizeof(int) * rows * cols);
    printf("array starts at %p\n", array);
    printf("sizeof(array) = %li\n", sizeof(array));
    printf("sizeof(array[0][0]) = 0x%lx\n", sizeof(array[0][0]));
    puts("");

    unsigned short r, c;

    for (r = 0; r <= rows - 1; r++) {
        for (c = 0; c <= cols - 1; c++) {
            printf("array[%i][%i] is at %p\n", r, c, &(*array[r][c]));
        };
        puts("");
    };
}

Although it compiles without warnings, it has some unexpected output:

sizeof(int) = 4

array starts at 0x16cc010
sizeof(array) = 8
sizeof(array[0][0]) = 0x14

array[0][0] is at 0x16cc010
array[0][1] is at 0x16cc024
array[0][2] is at 0x16cc038
array[0][3] is at 0x16cc04c
array[0][4] is at 0x16cc060

array[1][0] is at 0x16cc04c
array[1][1] is at 0x16cc060
array[1][2] is at 0x16cc074
array[1][3] is at 0x16cc088
array[1][4] is at 0x16cc09c

array[2][0] is at 0x16cc088
array[2][1] is at 0x16cc09c
array[2][2] is at 0x16cc0b0
array[2][3] is at 0x16cc0c4
array[2][4] is at 0x16cc0d8

I don't really understand what's happening here. The elements are contiguous, but they're way too large for ints, and the array is way too small to hold all of them.

My array declaration is the part I'm most unsure about — I'm reading int (*array)[rows][cols] as "array is a pointer to an array (with stride cols) of ints", but apparently that's incorrect.

What exactly am I doing wrong here?

解决方案

int (*array)[cols] = malloc( sizeof *array * rows );

for (r = 0; r <= rows - 1; r++) {
    for (c = 0; c <= cols - 1; c++) {
        printf("array[%i][%i] is at %p\n", r, c, &array[r][c]);

This allows you to index array as array[i][j], since array[i] implicitly dereferences the pointer.

To deallocate, you just need to call

free( array );

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

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