本文介绍了赋值在C 3D阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个数组如下: -

  INT **数据;
INT ***计数;

运行某些分析之后,我想作以下分配 -

 算[I] [J] [K] =数据[I] [J]。

不过,我不断收到分段故障我认为这是相关的一些指针赋值问题 - 任何人都可以建议我怎么能做出这种分配

的典型值 -
    数据[I] [J] = 0/1/2。

ZALLOC的定义:

 的#define ZALLOC(项目,N型)如果((项目=(类型*)释放calloc((N),的sizeof(型)))== NULL)fatalx(无法为项目\\ n分配%d个单元(S),N)//存储器分配
INT **数据;
INT nrow,NCOL;ZALLOC(数据,NCOL,为int *);对于(指数= 0;指数 - LT; NCOL;指数++)
{
    ZALLOC(数据[指数],nrow,INT);
}INT G = 0,指数1 = 0,索引2 = 2;
数据[索引1] [索引2] =克;
INT ***计数;
INT DIM1 = 100,DIM2 = 1,为dim3 = 2;
ZALLOC(计数,DIM1,INT **);对于(i = 0; I< D​​IM1,我++)
{
ZALLOC(计数[I],DIM2,为int *);
为(J = 0; J< D​​IM2; J ++)
{
ZALLOC(计数[一] [J],为dim3,INT);
}
}//初始化
对于(i = 0; I< D​​IM1,我++)
{
    为(J = 0; J< D​​IM2; J ++)
{
    对于(K = 0; K<为dim3; k ++)
    {
        算上[I] [J] [K] = 0;
    }
}
}
//分配
算[0] [1] [2] =数据[1] [2];


解决方案

您的分配组织是一个有点奇怪。如果你有3个维度(让我们称之为他们的水平,行和列),你通常会分配空间来保存的水平,然后对每个级别,你会分配空间层次中的行,然后最后你将分配空间的行中的列。

您code似乎在中间(行)启动;那么在做一些水平工作;终于在列。

这code是你的一个完全重写,但它的作品没有崩溃。我还没有与的valgrind 验证它;我需要升级这台机器上的版本。

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;#定义ZALLOC(项目,N型)如果((项目=(类型*)释放calloc((N),的sizeof(型)))== NULL)\\
                                  fatalx(无法为项目\\ n分配%d个单元(S),N)静态无效fatalx(为const char * str中,为size_t N)
{
    fprintf中(标准错误,%S:%祖\\ n,STR,N);
    出口(1);
}静态INT *** alloc_3d(INT水平,诠释行,诠释COLS)
{
    诠释计数= 0;
    INT *** array_3d;
    ZALLOC(array_3d,水平,INT **);
    的for(int i = 0; I<的水平;我++)
    {
        INT **数据;
        ZALLOC(数据,行,为int *);
        array_3d [I] =数据;
        对于(INT J = 0; J<行; J ++)
        {
            INT *条目;
            ZALLOC(条目,COLS,INT);
            array_3d [I] [J] =条目;
            对于(INT K = 0; K< COLS; k ++)
            {
                array_3d [I] [J] [K] =计数++;
            }
        }
    }
    返回array_3d;
}静态无效print_3d(INT *** A3D,整数水平,诠释行,诠释COLS)
{
    的for(int i = 0; I<的水平;我++)
    {
        的printf(%D:\\ n,I);
        对于(INT J = 0; J<行; J ++)
        {
            的printf(%D:,J);
            对于(INT K = 0; K< COLS; k ++)
                的printf(%3D,A3D [I] [J] [K]);
            的putchar('\\ n');
        }
    }
}静态无效free_3d(INT *** A3D,整数水平,诠释行)
{
    的for(int i = 0; I<的水平;我++)
    {
        对于(INT J = 0; J<行; J ++)
            免费(A3D [I] [J]);
        免费(A3D [I]);
    }
    免费(A3D);
}INT主要(无效)
{
    INT D1 = 3;
    INT D2 = 5;
    INT D3 = 7;
    INT *** A3D = alloc_3d(D1,D2,D3);    print_3d(A3D,D1,D2,D3);
    free_3d(A3D,D1,D2);    返回(0);
}

输出:

  0:
   0:0 1 2 3 4 5 6
   1:7 8 9 10 11 12 13
   2:14 15 16 17 18 19 20
   3:21 22 23 24 25 26 27
   4:28 29 30 31 32 33 34
1:
   0:35 36 37 38 39 40 41
   1:42 43 44 45 46 47 48
   2:49 50 51 52 53 54 55
   3:56 57 58 59 60 61 62
   4:63 64 65 66 67 68 69
2:
   0:70 71 72 73 74 75 76
   1:77 78 79 80 81 82 83
   2:84 85 86 87 88 89 90
   3:91 92 93 94 95 96 97
   4:98 99 100 101 102 103 104

I have 2 arrays as follows -

int **data;
int ***count;

After running some analysis, I want make the following assignment-

count[i][j][k] = data[i][j];

However, I keep getting Segmentation fault which I think is related to some pointer assignment issues - Can anyone suggest how I can make this assignment?

Typical values of - data[i][j] = 0/1/2.

Definition of ZALLOC:

#define ZALLOC(item,n,type)      if ((item = (type *)calloc((n),sizeof(type))) == NULL)    fatalx("Unable to allocate %d unit(s) for item\n",n)

// memory assignment
int **data;
int nrow, ncol;

ZALLOC(data, ncol, int *);

for (index = 0; index < ncol; index++)
{
    ZALLOC(data[index], nrow, int);
}

int g=0, index1=0, index2=2;
data[index1][index2] = g;


int ***count;
int dim1 = 100, dim2 = 1, dim3=2;
ZALLOC(count, dim1, int **);

for (i = 0; i < dim1; i++)
{
ZALLOC(count[i], dim2, int *);
for (j = 0; j < dim2; j++)
{
ZALLOC(count[i][j], dim3, int);
}
}

// Initialize
for (i = 0; i < dim1; i++)
{
    for (j = 0; j < dim2; j++)
{
    for (k = 0; k < dim3; k++)
    {
        count[i][j][k] = 0;
    }
}
}
// Assignment
count[0][1][2] = data[1][2];
解决方案

Your organization of the allocations is a bit odd. If you have 3 dimensions (lets call them levels, rows, and columns), you would normally allocate space to hold the levels, and then for each level you would allocate the space for the rows within the level, and then finally you would allocate the space for the columns within the row.

Your code seems to start in the middle (rows); then does some work at levels; and finally at the columns.

This code is a complete rewrite of yours, but it works without crashing. I've not yet validated it with valgrind; I need to upgrade the version on this machine.

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

#define ZALLOC(item, n, type) if ((item = (type *)calloc((n), sizeof(type))) == NULL) \
                                  fatalx("Unable to allocate %d unit(s) for item\n", n)

static void fatalx(const char *str, size_t n)
{
    fprintf(stderr, "%s: %zu\n", str, n);
    exit(1);
}

static int ***alloc_3d(int levels, int rows, int cols)
{
    int count = 0;
    int ***array_3d;
    ZALLOC(array_3d, levels, int **);
    for (int i = 0; i < levels; i++)
    {
        int **data;
        ZALLOC(data, rows, int *);
        array_3d[i] = data;
        for (int j = 0; j < rows; j++)
        {
            int *entries;
            ZALLOC(entries, cols, int);
            array_3d[i][j] = entries;
            for (int k = 0; k < cols; k++)
            {
                array_3d[i][j][k] = count++;
            }
        }
    }
    return array_3d;
}

static void print_3d(int ***a3d, int levels, int rows, int cols)
{
    for (int i = 0; i < levels; i++)
    {
        printf("%d:\n", i);
        for (int j = 0; j < rows; j++)
        {
            printf("   %d:  ", j);
            for (int k = 0; k < cols; k++)
                printf(" %3d", a3d[i][j][k]);
            putchar('\n');
        }
    }
}

static void free_3d(int ***a3d, int levels, int rows)
{
    for (int i = 0; i < levels; i++)
    {
        for (int j = 0; j < rows; j++)
            free(a3d[i][j]);
        free(a3d[i]);
    }
    free(a3d);
}

int main(void)
{
    int d1 = 3;
    int d2 = 5;
    int d3 = 7;
    int ***a3d = alloc_3d(d1, d2, d3);

    print_3d(a3d, d1, d2, d3);
    free_3d(a3d, d1, d2);

    return(0);
}

Output:

0:
   0:     0   1   2   3   4   5   6
   1:     7   8   9  10  11  12  13
   2:    14  15  16  17  18  19  20
   3:    21  22  23  24  25  26  27
   4:    28  29  30  31  32  33  34
1:
   0:    35  36  37  38  39  40  41
   1:    42  43  44  45  46  47  48
   2:    49  50  51  52  53  54  55
   3:    56  57  58  59  60  61  62
   4:    63  64  65  66  67  68  69
2:
   0:    70  71  72  73  74  75  76
   1:    77  78  79  80  81  82  83
   2:    84  85  86  87  88  89  90
   3:    91  92  93  94  95  96  97
   4:    98  99 100 101 102 103 104

这篇关于赋值在C 3D阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 08:04