4维数组-C

发布于 2025-02-06 22:01:29 字数 378 浏览 2 评论 0原文

以下代码有什么问题? C编译器向我显示错误:分段故障

#include <stdio.h> 
#include <math.h>

int main() {
    int n = 4;
    float A[n][n][n][n];
    A[n][n][1][1] = 1;
    A[n][n][1][2] = 0;
    A[n][n][1][3] = -3;
    A[n][n][1][4] = 0;
    A[n][n][2][1] = 1;
    A[n][n][2][2] = 0;
    A[n][n][2][3] = -3;
    A[n][n][2][4] = 0;
    return 0;
}

What is the problem with the following code? C compiler shows me error: Segmentation fault.

#include <stdio.h> 
#include <math.h>

int main() {
    int n = 4;
    float A[n][n][n][n];
    A[n][n][1][1] = 1;
    A[n][n][1][2] = 0;
    A[n][n][1][3] = -3;
    A[n][n][1][4] = 0;
    A[n][n][2][1] = 1;
    A[n][n][2][2] = 0;
    A[n][n][2][3] = -3;
    A[n][n][2][4] = 0;
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

×眷恋的温暖 2025-02-13 22:01:29

所有这些分配访问定义变量长度阵列之外的访问存储器。

A[n][n][1][1]=1;
A[n][n][1][2]=0;
A[n][n][1][3]=-3;
A[n][n][1][4]=0;
A[n][n][2][1]=1;
A[n][n][2][2]=0;
A[n][n][2][3]=-3;
A[n][n][2][4]=0;

这是数组每个维度的有效索引范围[0,n)。因此,表达式a [n]是无效的。

看来您的意思是以下任务

A[0][0][0][0]=1;
A[0][0][0][1]=0;
A[0][0][0][2]=-3;
A[0][0][0][3]=0;
A[0][0][1][0]=1;
A[0][0][1][1]=0;
A[0][0][1][2]=-3;
A[0][0][1][3]=0;

All these assignments access memory outside the defined variable length array.

A[n][n][1][1]=1;
A[n][n][1][2]=0;
A[n][n][1][3]=-3;
A[n][n][1][4]=0;
A[n][n][2][1]=1;
A[n][n][2][2]=0;
A[n][n][2][3]=-3;
A[n][n][2][4]=0;

That is the valid range of indices for each dimension of the array is [0, n ). Thus the expression A[n] is invalid.

It seems you mean the following assignments

A[0][0][0][0]=1;
A[0][0][0][1]=0;
A[0][0][0][2]=-3;
A[0][0][0][3]=0;
A[0][0][1][0]=1;
A[0][0][1][1]=0;
A[0][0][1][2]=-3;
A[0][0][1][3]=0;
跨年 2025-02-13 22:01:29

如果定义长度为n的数组,则对最后一个元素的访问在N-1索引处,并且对第一个元素的访问在索引0处,因为索引从0开始,在n-1结束。

因此,您的代码应该是这样的:

#include<stdio.h> 
#include<math.h>
#define N 4

int main() {
    float A[N][N][N][N];
    A[N-1][N-1][0][0]=1;
    A[N-1][N-1][0][1]=0;
    A[N-1][N-1][0][2]=-3;
    A[N-1][N-1][0][3]=0;
    A[N-1][N-1][1][0]=1;
    A[N-1][N-1][1][1]=0;
    A[N-1][N-1][1][2]=-3;
    A[N-1][N-1][1][3]=0;
    return 0;
}

另外,您使用一个变量来定义数组的长度,仅在 c99标准及以后。如果您不需要 vla> vla (variable长度阵列),您应该使用#define而不是定义。

If you define an array of length N, the access to the last element is at the N-1 index, and the access to the first element is at index 0 because indexes start from 0 and end at N-1.

So your code should be like this:

#include<stdio.h> 
#include<math.h>
#define N 4

int main() {
    float A[N][N][N][N];
    A[N-1][N-1][0][0]=1;
    A[N-1][N-1][0][1]=0;
    A[N-1][N-1][0][2]=-3;
    A[N-1][N-1][0][3]=0;
    A[N-1][N-1][1][0]=1;
    A[N-1][N-1][1][1]=0;
    A[N-1][N-1][1][2]=-3;
    A[N-1][N-1][1][3]=0;
    return 0;
}

Also, you're using a variable to define the length of your array, which is good only in the C99 standard and onwards. If you don't need a VLA (variable length array), you should use the #define instead.

初雪 2025-02-13 22:01:29
int n=4;
float A[n][n][n][n];
A[n][n][1][1]=1;

在每个C标准中都不有效(阅读或更好)。您可能需要代码const int n = 4;#define n 4

(在廉价的微控制器上,您甚至可能没有足够的堆栈空间来容纳256个浮点溢出)

然后, you 始终有一个 buffer vellflow (阅读有关 undected行为)。在a [x] [x] [1] [1]中,有效索引来自x为0至x是3- N -1 - (不是4)。

您可能需要代码

A[n-1][n-1][1][1] = 1;

,但您应该知道a [0] [0] [0] [0]正在非传统化。它可能包含垃圾(信号nan)和假设的printf(“%f \ n”,a [0] [0] [0] [0] [0]);可能会失败....

i猜猜 frama-c 会遇到您的错误(我会让您检查一下)。请注意 rice的定理

我将使用零(以简化调试,并具有更可重复的运行)初始化a - 在纠正了缓冲区溢出之后:

float A[n][n][n][n]={0.0};
int n=4;
float A[n][n][n][n];
A[n][n][1][1]=1;

is not valid in every C standard (read n1570 or better). You may want to code const int n=4; or #define n 4

(on cheap microcontrollers you might not even have enough stack space for 256 floats -> possible stack overflow)

And then, you always have a buffer overflow (read about undefined behavior). In A[x][x][1][1] the valid indexes are from x being 0 to x being 3 -that is n-1 - (not 4).

You might want to code

A[n-1][n-1][1][1] = 1;

but you should know that A[0][0][0][0] is being uninitialized. It may contain garbage (signalling NaN), and an hypothetical printf("%f\n", A[0][0][0][0]); could fail ....

I guess that Frama-C would have caught your mistake (I leave you to check that). Be aware of Rice's theorem.

I would initialize A with zeros (to ease debugging, and to have more reproducible runs) -after having corrected the buffer overflow-:

float A[n][n][n][n]={0.0};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文