小编aeh*_*s29的帖子

从C++中的函数动态分配和修改内存

我想读取一个文件并分配一个包含读取值的数组,稍后我将使用它.

这是我得到的:

int main(){
float *values;

somefunction (&values);
values[3]=3;  //Works OK but I want to modify it from the other function

}

somefunction(float ** values){

//I read the file here and count the lines
//for the sake of simplicity lets say I got lines=10;

*values = new float[lines]; //Works OK
*values[0]=0;  //Works OK because it points to the first or only element
*values[1]=1;  //Segmentation Fault

}
Run Code Online (Sandbox Code Playgroud)

为什么从main函数修改新分配的浮点数组是可以的,而不是从分配它的同一函数中修改,我做错了什么?

我很确定我修改数组的语法是错误的,但我不知道为什么......,提前谢谢.

c++

1
推荐指数
1
解决办法
94
查看次数

标签 统计

c++ ×1