【发布时间】:2016-11-09 11:27:32
【问题描述】:
我必须创建一个矩阵,其宽度和高度由从写入文件中获得的两个参数确定。但是,在某些情况下,当矩阵太大时,我有一个segmentation fault。我认为可能是因为我正在以静态方式创建矩阵,所以我需要动态创建它,但是这里出现了我的问题,因为我不知道该怎么做。
我现在的代码是这样的:
FILE * fp;
unsigned int width=0;
unsigned int height=0;
//Open the file. argv[4] parameter contains the file
fp=fopen (argv[4],"r");
//Go to the last position which indicates the size
fseek(fp, 0, SEEK_END);
//Return to the start:
rewind(fp);
//The value of the first 4 bytes represent the width
size_t return1 = fread(&width,4,1,fp);
//The value of the next 4 bytes represent the height
size_t return2 = fread(&height,4,1,fp);
//Matrix creation
if (return1 > 0 && return2 > 0) {
unsigned int matrix[width][height];
【问题讨论】:
-
您是如何设法发布带有
heigth错字的代码的?不管怎样,开始吧: 1. 用 C++ 做事,就像用 C++ 做的一样 2. 使用 Google -
@LogicStuff 已编辑。抱歉,输入有误
标签: c++ c++11 matrix segmentation-fault malloc