- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的第一篇文章,如果它不符合我应该发布的标准,如果我放置代码的方式有任何问题,请说出来,我会在以后的帖子中考虑到这一点。我尝试尽可能多地注释代码,也许有点太多了。
所以代码的想法是采用一个普通矩阵,然后以简化形式存储它,这工作正常,但是当我也希望将其与 vector 相乘时,它会失败。
代码失败了,因为当我使用 if 语句执行双 for 循环时,它会分段,而且我不知道为什么。
如果我单独指定元素,它可以正常工作,但当我尝试在 if 语句内增加应该为 Matrix_R 选择的元素时,它会失败。
任何帮助将不胜感激。谢谢!!
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int i;
int j;
int n;
int m;
int k=0;
double * Vin;
double * Vout_M;
double * Vout_MR;
double ** Matrix;
double ** Matrix_R;
//Vout_M and Vout_MR are output vectors for the two possible inputted matricies
//Matrix_R is the reduced form of Matrix
n=4;
Vin = malloc(n*sizeof(double));
Vout_M = malloc(n*sizeof(double));
Vout_MR = malloc(n*sizeof(double));
Matrix = malloc(n*sizeof(double*));
for(i=0;i<n;i++)
Matrix[i] = malloc(n*sizeof(double));
//Allocates memory for the arrays
for(i=0;i<n;i++){
Vin[i]=1;
Vout_M[i]=0;
Vout_MR[i]=0;
}
//Initiates the vector arrays
for(i=0;i<n;i++)
for(j=0;j<n;j++)
Matrix[i][j]=0;
//Initiates the Matrix
Matrix[2][3]=5;
Matrix[0][2]=10;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(Matrix[i][j]!=0)
m++;
//Scans through to determine what size the reduced array should be
//In Future, this could be set up to read from a text file, and so RAM isn't used to store the array to find elements
Matrix_R = malloc(m*sizeof(double*));
for(i=0;i<m;i++)
Matrix_R[i] = malloc(3*sizeof(double));
for(i=0;i<m;i++)
for(j=0;j<3;j++)
Matrix_R[i][j]=0;
//Produces the reduced array, and initiates it
printf("\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(Matrix[i][j]!=0){
Matrix_R[k][0]=Matrix[i][j];
Matrix_R[k][1]=i;
Matrix_R[k][2]=j;
k++;
}
//Scans through the Matrix array and outputs to the Matrix_R array
for(i=0;i<k;i++)
printf("Matrix_R[%d][0] = %lf, Matrix_R[%d][1] = %lf, Matrix_R[%d][2] = %lf\n",i,Matrix_R[i][0],i,Matrix_R[i][1],i,Matrix_R[i][2]);
//Checks if it is storred correctly -- this outputs fine
//Now first do the multiplication between the normal matrix and vector --- Matrix * Vector
for(i=0;i<n;i++)
for(j=0;j<n;j++)
Vout_M[i] += Matrix[i][j]*Vin[j];
for(i=0;i<n;i++)
printf("\nVout_M[%d] = %lf",i,Vout_M[i]);
printf("\n");
//Prints the output from a standard Matrix * Vin in the form of Vout_M
k=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++){
if(i == Matrix_R[k][1] && j == Matrix_R[k][2]){
Vout_MR[i] += Matrix_R[k][0]*Vin[j];
k++;
}}
//Goes through standard matrix-vector multiplication, using reduced matrix
//SEGMENTS :: When trying to go through an if statement and incrementing k.
//i.e. If I set k=int, and then not increase it in the if statement it works.
for(i=0;i<n;i++)
printf("\nVout_MR[%d] = %lf",i,Vout_MR[i]);
printf("\n");
//Outputs the Vout_MR which is from Matrix_R * VIN
free(Vin);
free(Vout_M);
free(Vout_MR);
for(i=0;i<n;i++)
free(Matrix[i]);
for(i=0;i<m;i++)
free(Matrix_R[i]);
return 0;
}
最佳答案
在您的代码中,您从未初始化
int m;
并且您正在使用未初始化的值
if(Matrix[i][j]!=0)
m++;
和
Matrix_R = malloc(m*sizeof(double*));
它产生一个先读后写场景。该行为未定义。
此外,请始终检查 malloc()
的返回值以确保成功。
您可以[并且应该]使用%f
格式说明符来打印 double 值。改变
printf("\nVout_M[%d] = %lf",i,Vout_M[i]);
至
printf("\nVout_M[%d] = %f",i,Vout_M[i]);
接下来,您已free()
释放了所有Matrix[i]
和Matrix_R[i]
,但您忘记了free()
Matrix
和 Matrix_R
本身。
编辑
在您的代码中,没有对 k
中的值进行边界检查
if(i == Matrix_R[k][1] && j == Matrix_R[k][2])
一旦您的 k >= m
,您将访问超出范围的内存,从而调用未定义的行为。副作用是段错误。
在您的情况下,m
的值很可能是 2
,并且当 k
变为 2
时通过k++
,对i == Matrix_R[k][1]
的下一次访问会生成越界内存访问。
关于c - 在 C 中的 for 循环和 if 语句中使用 Malloc 数组 - 导致分段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28407821/
最近,我读了一本名为Understanding the linux kernel 的书。有一句话让我很困惑。谁能给我解释一下? As stated earlier, the Current Privi
Julia中是否有任何类型的分段/非连续范围的对象类?例如,我可以创建一个常规范围: a = UnitRange(1:5) 但是,如果我想将此与其他范围相结合: b = UnitRange([1:5,
我一直在研究标准输入的修复后计算器,在研究它之后,我最终得到了一个说法“段错误(核心转储)”,它没有说明它发生的位置或导致它的原因,并在寻找解释后我真的找不到任何可以帮助我解决这个问题的东西。所以我决
我对创建应用程序还很陌生,我刚开始使用 Swift 进行 iOS 开发。我真的希望有人能花点时间帮助我,因为我现在很困。所以我正在创建一个带有主从 TableView 的应用程序。我将使用 Alamo
ScrollView 或类似的东西中的 UIView、SegmentedController 和 UIContainerView? 在我的 Storyboard中,我有一个 VC,它在顶部包含一个 U
我需要构建一个具有任意数量的间隔和函数的分段函数,能够对输入的 numpy 数组进行操作。 我可以使用 for 循环和指示符数组来做到这一点,如下面的代码片段所示,但是有没有一种更 Pythonic
我正在尝试按照提交的方式输出我的输出,但它不起作用。比如说,如果我提交一个分为两段的帖子(通过按回车键将它们分开)它不会那样输出。它删除该段落并将帖子显示为一个段落。 显示结果: 我正在尝试按照提交的
我有一个 TableView Controller ,其中按字母顺序列出了美国所有州。单击某个状态会通过 Web 服务调用返回有关该状态的详细信息。这是我第一次尝试分段或分组 TableView ,我
我想创建一个链接节点列表。这个想法是用字母分隔一个单词,并通过每个不同的字母创建一个链接节点列表。我也尝试在最后一个位置添加最后一个节点。 我已经尝试了很多东西,理论上是可行的,但我找不到段错误的根源
这是我正在尝试创建的附加组件: import bpy import os import sys import subprocess import time from threading import
我觉得这应该是一个简单的属性,但我一直在搜索,但找不到答案。 我的表右侧有一个 TableView 索引。选择后,它会使所有内容变灰。我想模仿 iPod 应用程序,当索引本身被选中时,它是半透明的(可
我试图在 OpenCV 中使用 SLIC 分割图像。我正在尝试使用以下功能: void vl_slic_segment ( vl_uint32 * segmentation, flo
我知道在网络中,数据流在第4层被分成不同的段。每个段然后用端口号和IP地址封装。我发现了一些我想问的令人困惑的问题: 谁负责将数据流分成不同的段。是应用程序还是第 4 层? 我知道 UDP 不支持分段
我有一个使用界面生成器制作的分段 Controller ,它看起来像这样 有时我使用它将它设置为四个 [segmentedControl insertSegmentWithTitle:@"Dinner
所以我使用免费托管,我的服务器上几乎没有视频,一切正常,除了视频需要大约 10-20 秒来加载然后开始播放,所以我想使用流媒体。问题是我不知道该字段和托管不支持 node.js,但我认为是 php 7
ViewController 有三个按钮:“ friend ”“日历”“信息”。每个按钮将代表此 viewController 下半部分的不同信息。 我的问题是选择什么来表示这些信息 - 分段 Con
在我的应用程序中,弹出窗口中的 UITableView 会在新数据时重新加载。如果表格未分区,则使用此代码一切都可以完美运行: 通知,启动重新加载: - (void)recieveNotificati
在阅读muduo(C++网络库)的源码时提出了这个问题。 如果客户端发送一个大消息,它将被 TCP 分段,服务器端会发生什么? (服务器是否知道这条消息已经被分段了?) 网络库是否需要等待整个消息并且
我正在重构一个包含大量长头文件的代码库(为了便于使用和简化编译,头文件同时包含接口(interface)和实现)。 代码库避免了像野火一样的多态性,因此它使用宏解析内部存储类型,如下所示: #if d
我正在阅读有关 x86 保护模式工作的信息,因为我已经看到了平面内存模型和分段内存模型。 如果 linux 内核使用平面内存模型,那么它如何保护非特权应用程序对关键数据的访问? 最佳答案 Linux
我是一名优秀的程序员,十分优秀!