在1D和2D阵列之间进行转换 [英] Casting between 1D and 2D arrays

查看:140
本文介绍了在1D和2D阵列之间进行转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


考虑一下设置 M = N * N个整数。在我的程序的某些部分,我想要

来处理这个集合作为M个整数的一维数组。在我的

程序的其他部分,我想将此设置作为N * N整数的2D矩阵处理。


以下代码100%合法C89代码?


#include< assert.h>

#define N 9

typedef int row_t [N];


无效比较(int *数组)

{

int i,j;

row_t * matrix =(row_t *)array;

for(i = 0; i< N; ++ i)

for(j = 0; j< N ; ++ j)

{

断言(& matrix [i] [j] ==& array [i * N + j]);

断言(矩阵[i] [j] ==数组[i * N + j]);

}

}


int main(无效)

{

int i;

int foo [N * N];

for(i = 0; i< N * N; ++ i)foo [i] = 7 * i + 11;

比较(foo);

返回0;

}


我可以确定断言永远不会失败吗?


问候。

解决方案

Boon< root @ localhostwrites:

考虑一个集合 M = N * N个整数。在我的程序的某些部分,我想要将这个集合作为M个整数的一维数组来处理。在我的程序的其他部分中,我想把这个集合作为N * N整数的2D矩阵处理。


以下代码100%是合法的C89代码?



我想是的,是的。我希望别人能先把他们的脖子伸出来b / b
,但也可能是我!请注意,100%合法!= 100%

便携式 - 我看到一个可能的问题。


#include< assert.h>

#define N 9

typedef int row_t [N];


void compare(int * array)

{

int i,j;

row_t * matrix =(row_t *)array;



唯一的问题似乎是这次转换。除了必须正确对齐指针外,标准说

非常少。

我认为row_t可能有更严格的对齐

要求比int,但如果指针

通过malloced而不是从声明的数组转换为

in main(malloced空间必须正确对齐),这个问题就会消失任何使用)。


需要这种对齐的实现确实很奇怪

因为它只适用于某些大小的row_t,但我可以' 'b

说服自己标准禁止它。


for(i = 0; i< N; ++ i)

for(j = 0; j< N; ++ j)

{

断言(& matrix [i] [j] ==& array [i * N + j]);

断言(matrix [i] [j] == array [i * N + j]);

}

}


int main(无效)

{

int i;

int foo [N * N];

for(i = 0;我< N * N; ++ i)foo [i] = 7 * i + 11;

比较(foo);

返回0;

}


我可以确定断言永远不会失败吗?



单凭这还不够。 UB(如果存在)可能导致断言

传递事实上,他们失败。如果数组是
malloced我认为你没问题,或者你的目标实现没有奇怪的对齐

阵列数组的要求。


-

Ben。



void compare(int * array)

{

int i,j;

row_t * matrix =(row_t *)array;

for(i = 0; i< N; ++ i)

for(j = 0; j< N; ++ j)

{

断言(& matrix [i] [j] ==& array [i * N + j]);

断言(matrix [i] [j] == array [i * N + j $);

}

}



这是合法的。


我可以确定断言永远不会失败吗?



据我所知是的。


我认为row_t可能有更严格的对齐方式

要求比int



我不明白为什么,因为row_t是一个整数数组。据我所知

ints不需要在大多数架构上保持一致,它们只是更快

。在需要对齐的任何架构上

foo [N * N]将在堆栈上分配时对齐。


< a href =mailto:da ******* @ gmail.com> da ******* @ gmail.com 写道:


[你已经失去了归因并混淆了报价水平。请

如果再次发布,请尽量不这样做。]


> void compare(int * array)
{i /,j;
row_t * matrix =(row_t *)array;
for(i = 0; i< N; ++ i)
for(j = 0; j 断言(& matrix [i] [j] ==& array [i * N + j ]);
断言(矩阵[i] [j] ==数组[i * N + j]);
}
}



这是合法的。


>我可以确定断言永远不会失败吗?



据我所知是的。


>我认为row_t可能有更严格的对齐
要求比int



我写了这个(但不是用>>引用的其他东西)。


我不明白为什么,因为row_t是一个整数数组。据我所知

ints不需要在大多数架构上保持一致,它们只是更快

。在需要对齐的任何架构上

foo [N * N]将在堆栈上分配时对齐。



是的,但我的观点是关于一些需要更多对齐的数组指针

比其他人更多。我不能想到为什么地球上会这样,但是

我不能用标准中的引号显示它永远不会是

案例。


-

Ben。


Hello everyone,

Consider a "set" of M=N*N integers. In some parts of my program, I want
to handle this set as a 1D array of M integers. In other parts of my
program, I want to handle this set as a 2D matrix of N*N integers.

Is the following code 100% legal C89 code ?

#include <assert.h>
#define N 9
typedef int row_t[N];

void compare(int *array)
{
int i, j;
row_t *matrix = (row_t *)array;
for (i=0; i < N; ++i)
for (j=0; j < N; ++j)
{
assert( &matrix[i][j] == &array[i*N+j] );
assert( matrix[i][j] == array[i*N+j] );
}
}

int main(void)
{
int i;
int foo[N*N];
for (i=0; i < N*N; ++i) foo[i] = 7*i+11;
compare(foo);
return 0;
}

Can I be sure that the asserts will never fail ?

Regards.

解决方案

Boon <root@localhostwrites:

Consider a "set" of M=N*N integers. In some parts of my program, I
want to handle this set as a 1D array of M integers. In other parts of
my program, I want to handle this set as a 2D matrix of N*N integers.

Is the following code 100% legal C89 code ?

I think so, yes. I was hoping someone else would stick their neck out
first, but it may as well be me! Note that 100% legal != 100%
portable -- I see one possible gotcha.

#include <assert.h>
#define N 9
typedef int row_t[N];

void compare(int *array)
{
int i, j;
row_t *matrix = (row_t *)array;

The only issues seems to be to be this conversion. The standard says
very little about it except that the pointer must be properly aligned.
I think it is possible for row_t to have stricter alignment
requirements than int, but this problem would vanish if the pointer
passed were malloced rather than being converted from a declared array
in main (malloced space must be correctly aligned for any use).

An implementation that requires such an alignment would be odd indeed
since it would only apply to certain sizes of row_t, but I can''t
persuade myself that the standard prohibits it.

for (i=0; i < N; ++i)
for (j=0; j < N; ++j)
{
assert( &matrix[i][j] == &array[i*N+j] );
assert( matrix[i][j] == array[i*N+j] );
}
}

int main(void)
{
int i;
int foo[N*N];
for (i=0; i < N*N; ++i) foo[i] = 7*i+11;
compare(foo);
return 0;
}

Can I be sure that the asserts will never fail ?

That alone is not enough. UB (if present) could cause the asserts to
"pass" when in fact they "fail". I think you are OK if the array is
malloced or you target implementations have no bizarre alignment
requirements for arrays of arrays.

--
Ben.


void compare(int *array)
{
int i, j;
row_t *matrix = (row_t *)array;
for (i=0; i < N; ++i)
for (j=0; j < N; ++j)
{
assert( &matrix[i][j] == &array[i*N+j] );
assert( matrix[i][j] == array[i*N+j] );
}
}

That is legal.

Can I be sure that the asserts will never fail ?

As far as I know yes.

I think it is possible for row_t to have stricter alignment
requirements than int

I don''t see why, as row_t is an array of ints. And as far as I know
ints don''t NEED to be aligned on most architectures, it''s just faster
when they are. On any architecture where alignment is necesary
foo[N*N] will be aligned when allocated on the stack.


da*******@gmail.com writes:

[You''ve lost the attributions and confused the quote levels. Please
try not to do that if you post again.]

>void compare(int *array)
{
int i, j;
row_t *matrix = (row_t *)array;
for (i=0; i < N; ++i)
for (j=0; j < N; ++j)
{
assert( &matrix[i][j] == &array[i*N+j] );
assert( matrix[i][j] == array[i*N+j] );
}
}


That is legal.

>Can I be sure that the asserts will never fail ?


As far as I know yes.

>I think it is possible for row_t to have stricter alignment
requirements than int

I wrote this (but not the other stuff quoted with ">>").

I don''t see why, as row_t is an array of ints. And as far as I know
ints don''t NEED to be aligned on most architectures, it''s just faster
when they are. On any architecture where alignment is necesary
foo[N*N] will be aligned when allocated on the stack.

Yes, but my point was about some array pointers needing more alignment
than others. I can''t think why on earth this would ever be done, but
I can''t show, using quotes from the standard, that it is never the
case.

--
Ben.


这篇关于在1D和2D阵列之间进行转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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