我必须创建一个矩阵,其宽度和高度由写入文件时获得的两个参数决定。但是,在矩阵太大的情况下,我有segmentation fault
。我认为可能是因为我以静态的方式创建矩阵,所以我需要动态创建它,但是在这里出现我的问题,因为我不知道该怎么做。 我的代码,现在是这样的:在C++中动态创建一个数组
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
@LogicStuff编辑。对不起,输入错误 – giorgioW