Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C+中的神秘分段错误+;代码 自从我编码C++以来,我已经有一段时间了,这个代码示例有一些问题: #include <iostream> #include <string> #include <sstream> #include <vector> using namespace std; string* data; int current = 0; void init(){ data = new string[6]; data[0] = "7 4 3"; data[1] = "8 11 12 16 17 18 20"; data[2] = "17 16 20 2 20 5 13"; data[3] = "17 8 8 16 12 15 13"; data[4] = "12 4 16 4 15 7 6"; data[5] = "8 14 2 11 17 12 8"; } string getNext() { if(current == 0){ init(); } return data[current++]; } void populate(int* target, int n){ target = new int[n]; string line = getNext(); stringstream ss(line); for(int i=0; i<n; i++){ ss >> target[i]; cout << target[i] << endl; } // THIS WORKS FINE for(int i=0; i<n; i++){ cout << "TARGET" << target[i] << endl; } } int main() { string line; int n, s, e; int* x, * a, * b, * c, *d; line = getNext(); stringstream ss(line); ss >> n >> s >> e; cout << "N: " << n << " S: " << s << " E: " << e << endl; populate(x, n); // IMMEDIATE SEGMENTATION FAULT ON FIRST ITERATION for (int i = 0; i < n; i++) cout << x[i] << endl; populate(a, n); populate(b, n); populate(c, n); populate(d, n); return 0; } #包括 #包括 #包括 #包括 使用名称空间std; 字符串*数据; int电流=0; void init(){ 数据=新字符串[6]; 数据[0]=“7 4 3”; 数据[1]=“81112617181820”; 数据[2]=“17 16 20 2 20 5 13”; 数据[3]=“17 8 16 12 15 13”; 数据[4]=“12 4 16 4 15 7 6”; 资料[5]=“81421117128”; } 字符串getNext(){ 如果(当前==0){ init(); } 返回数据[current++]; } 无效填充(int*目标,int n){ 目标=新整数[n]; 字符串行=getNext(); 弦流ss(线); 对于(int i=0;i>target[i]; 库特; cout_C++_Memory - Fatal编程技术网 target[i]; 库特; cout,c++,memory,C++,Memory" /> target[i]; 库特; cout,c++,memory,C++,Memory" />

C+中的神秘分段错误+;代码 自从我编码C++以来,我已经有一段时间了,这个代码示例有一些问题: #include <iostream> #include <string> #include <sstream> #include <vector> using namespace std; string* data; int current = 0; void init(){ data = new string[6]; data[0] = "7 4 3"; data[1] = "8 11 12 16 17 18 20"; data[2] = "17 16 20 2 20 5 13"; data[3] = "17 8 8 16 12 15 13"; data[4] = "12 4 16 4 15 7 6"; data[5] = "8 14 2 11 17 12 8"; } string getNext() { if(current == 0){ init(); } return data[current++]; } void populate(int* target, int n){ target = new int[n]; string line = getNext(); stringstream ss(line); for(int i=0; i<n; i++){ ss >> target[i]; cout << target[i] << endl; } // THIS WORKS FINE for(int i=0; i<n; i++){ cout << "TARGET" << target[i] << endl; } } int main() { string line; int n, s, e; int* x, * a, * b, * c, *d; line = getNext(); stringstream ss(line); ss >> n >> s >> e; cout << "N: " << n << " S: " << s << " E: " << e << endl; populate(x, n); // IMMEDIATE SEGMENTATION FAULT ON FIRST ITERATION for (int i = 0; i < n; i++) cout << x[i] << endl; populate(a, n); populate(b, n); populate(c, n); populate(d, n); return 0; } #包括 #包括 #包括 #包括 使用名称空间std; 字符串*数据; int电流=0; void init(){ 数据=新字符串[6]; 数据[0]=“7 4 3”; 数据[1]=“81112617181820”; 数据[2]=“17 16 20 2 20 5 13”; 数据[3]=“17 8 16 12 15 13”; 数据[4]=“12 4 16 4 15 7 6”; 资料[5]=“81421117128”; } 字符串getNext(){ 如果(当前==0){ init(); } 返回数据[current++]; } 无效填充(int*目标,int n){ 目标=新整数[n]; 字符串行=getNext(); 弦流ss(线); 对于(int i=0;i>target[i]; 库特; cout

C+中的神秘分段错误+;代码 自从我编码C++以来,我已经有一段时间了,这个代码示例有一些问题: #include <iostream> #include <string> #include <sstream> #include <vector> using namespace std; string* data; int current = 0; void init(){ data = new string[6]; data[0] = "7 4 3"; data[1] = "8 11 12 16 17 18 20"; data[2] = "17 16 20 2 20 5 13"; data[3] = "17 8 8 16 12 15 13"; data[4] = "12 4 16 4 15 7 6"; data[5] = "8 14 2 11 17 12 8"; } string getNext() { if(current == 0){ init(); } return data[current++]; } void populate(int* target, int n){ target = new int[n]; string line = getNext(); stringstream ss(line); for(int i=0; i<n; i++){ ss >> target[i]; cout << target[i] << endl; } // THIS WORKS FINE for(int i=0; i<n; i++){ cout << "TARGET" << target[i] << endl; } } int main() { string line; int n, s, e; int* x, * a, * b, * c, *d; line = getNext(); stringstream ss(line); ss >> n >> s >> e; cout << "N: " << n << " S: " << s << " E: " << e << endl; populate(x, n); // IMMEDIATE SEGMENTATION FAULT ON FIRST ITERATION for (int i = 0; i < n; i++) cout << x[i] << endl; populate(a, n); populate(b, n); populate(c, n); populate(d, n); return 0; } #包括 #包括 #包括 #包括 使用名称空间std; 字符串*数据; int电流=0; void init(){ 数据=新字符串[6]; 数据[0]=“7 4 3”; 数据[1]=“81112617181820”; 数据[2]=“17 16 20 2 20 5 13”; 数据[3]=“17 8 16 12 15 13”; 数据[4]=“12 4 16 4 15 7 6”; 资料[5]=“81421117128”; } 字符串getNext(){ 如果(当前==0){ init(); } 返回数据[current++]; } 无效填充(int*目标,int n){ 目标=新整数[n]; 字符串行=getNext(); 弦流ss(线); 对于(int i=0;i>target[i]; 库特; cout,c++,memory,C++,Memory,罪魁祸首是: void populate(int* target, int n){ target = new int[n]; ... 在这里,您将target作为int*传递,然后立即将其本地重新分配到分配的内存中,与传入的指针的耦合将丢失。x因此仍然在main中统一,访问它是未定义的行为。将target的类型更改为int*& 你也可以使用int**并传递x的地址,但这需要在函数中对访问target进行大量更改。为什么你要使用所有这些数组和指针而不是std::vector?你从

罪魁祸首是:

void populate(int* target, int n){
    target = new int[n];
    ...

在这里,您将
target
作为
int*
传递,然后立即将其本地重新分配到分配的内存中,与传入的指针的耦合将丢失。
x
因此仍然在
main
中统一,访问它是未定义的行为。将
target
的类型更改为
int*&
你也可以使用
int**
并传递x的地址,但这需要在函数中对访问
target
进行大量更改。

为什么你要使用所有这些数组和指针而不是
std::vector
?你从来没有给
x
分配过任何内容,所以它是一个未初始化的pointer和
x[i]
是未定义的行为。
populate()
通过值接收
target
,因此分配给
target
不会修改
x
。如果您需要从函数中更改调用站点中的某个内容,则需要通过指针或引用传递该内容。您也没有这样做。“我有几个整数数组的指针,我将动态分配”:不要这样做,除非你正在练习或这是课程要求。你使用
#include
的方法是正确的。不要偏离路径。如果他更改为
int**
,他需要进行一系列其他相应的更改,如
*target=new int[n] 
。以及
*在
中为
循环设置目标[i]
。我会使此函数返回分配的数组。如果建议通过引用调用,可能会比较容易混淆这里没有“重新定义”!