本文介绍了获得“返回值3221226356”在C ++代码中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在编译一个示例代码。但是当我编译输入= 5的代码时,我得到了答案,如果我将输入从5更改为6.然后一旦答案到来,但下次相同输入时,抛出消息返回值3221226356。如果我再次将输入从6更改为8,则再次发出该消息。 #include < iostream > #include < string > #include < cmath > void loop1( float * a, int & k )\ { int i = k / 2 ; \ for ( int x = 0 ; x< i; x ++)\ {a [x] = x; \ std :: cout<< a =<< a [x]<< ' \ n'; \ } } void loop2( float * a, int & i, int & j)\ { for ( int x = i; x< j; x ++)\ {a [x] = a [xi]; \ std :: cout<< a [<< x< ;< ] =<< a [x]<< ' \ n'; \ } } void loop3( float * a , int & i) { for ( int x = 0 ; x< i; x ++) {a [x] = x * x * x; a [x] + = a [x]; } } int main()\ { int nx; \ float * xpos = NULL; \ xpos = new float [nx]; \ std :: cout<< 输入nx的值; \ std :: cin>> nx; \ int a = nx / 2; \ std :: cout<< a(= nx / 2)=<< a<< ' \ n'; \ loop1(xpos,nx); \ loop2(xpos,a,nx); \ for ( int x = 0 ; x< nx; x ++)\ { std :: cout<< xpos =<< xpos [x]<< ' \ n'; \ } / * loop3(xpos,nx); \ * / for ( int x = 0 ; x< nx; x ++)\ { std :: cout<< new xpos =<< xpos [x]<< ' \ n'; \ } 返回 0 ; } 输入结果= 5 输入值 nx 5 a(= nx / 2)= 2 a = 0 a = 1 a [ 2 ] = 0 a [ 3 ] = 1 a [ 4 ] = 0 xpos = 0 xpos = 1 xpos = 0 xpos = 1 xpos = 0 new xpos = 0 new xpos = 1 new xpos = 0 new xpos = 1 new xpos = 0 -------------------------------- 过程退出 4 。 442 秒,返回 值 0 按任意键继续 。 。 。 输入结果= 6 输入值 nx 6 a(= nx / 2)= 3 a = 0 a = 1 a = 2 a [ 3 ] = 0 a [ 4 ] = 1 a [ 5 ] = 2 xpos = 0 xpos = 1 xpos = 2 xpos = 0 xpos = 1 xpos = 2 new xpos = 0 new xpos = 1 new xpos = 2 new xpos = 0 new xpos = 1 new xpos = 2 ---------- ---------------------- 在 3 之后退出流程。 427 秒,返回 值 0 按任意键继续。 。 。 输入结果= 8 < pre lang =c ++>输入nx的值8 a(= nx / 2)= 4 a = 0 a = 1 a = 2 a = 3 a [4] = 0 a [5] = 1 a [6] = --------- ----------------------- 流程退出7.167秒后返回值3221226356 按任意键继续。 。 。 你对我做错了什么了解吗? 这个错误有没有具体名称? 但是,我找不到失败的原因。 我是什么尝试过: 我试图将输入从一个变为另一个。但是给出了消息。解决方案 int nx; \ float * xpos = NULL; \ xpos = new float [nx]; \ 你还没有为nx设置一个值,所以你的数组可能是-2,147,483,647和+2,147,483,647之间的任何大小。 除去那些反斜杠,您认为它们适用于什么? 查看您的代码: int main()\ { int nx; \ float * xpos = NULL; \ xpos = new float [nx]; 数组中分配了多少个浮点数?由于你没有为 nx 设置一个值(如果你很幸运,它取决于编译器和你给它的设置)默认为零。 由于它是一个局部变量,因此阵列在堆栈中。当您使用它时,您可以访问堆栈中根本不属于数组的部分,并且在写入数组时会损坏它们。此时,任何事情都可能发生! 为数组分配一些合适的大小,或者一旦知道需要多少元素就分配数组! 修复代码中的明显内容 #include < iostream > void loop1( float a [ ], size_t k) { size_t limit = k / 2 ; for ( size_t x = 0 ; x< limit; x ++) {a [x] = x; std :: cout<< a =<< a [x]<< ' \ n'; } } void loop2( float a [], size_t i, size_t j) { for ( size_t x = i; x< j; x ++) {a [x ] = a [xi]; std :: cout<< a [<< x<< ; ] =<< a [x]<< ' \ n'; } } void loop3( float a [], size_t i) { for ( size_t x = 0 ; x< i; x ++) {a [x] = x * x * x ; a [x] + = a [x]; } } int main() { int nx; float * xpos = NULL; std :: cout<< 输入nx的值 ; std :: cin>> nx; xpos = new float [nx]; int a = nx / 2; std :: cout<< a(= nx / 2)=< ;< a<< ' \ n'; loop1(xpos,nx); loop2(xpos,a,nx); for ( int x = 0 ; x< nx; x ++) { std :: cout<< xpos =<< xpos [x]<< ' \ n'; } / * loop3(xpos,nx); * / for ( int x = 0 ; x< nx; x ++) { std :: cout<< new xpos =<< xpos [x]<< ' \ n'; } delete [] xpos; } 您应该为您的功能提供有意义的名称。 您可以使用 std :: vector 而不是动态地为数组分配。 I am compiling one sample code. But I am getting the answer when I am compiling that code for input = 5, If I change input from 5 to 6., then once answer came, but next time for same input, throwing a message "return value 3221226356". If again I change the input from 6 to 8, then again that message is coming.#include <iostream>#include <string>#include <cmath>void loop1(float *a, int &k ) \{ int i = k / 2 ; \ for (int x = 0 ; x < i ; x++ ) \ { a[x] = x ; \ std::cout<<"a = "<< a[x] <<'\n' ; \ }}void loop2(float *a, int &i, int &j ) \{ for (int x = i ; x < j ; x++ ) \ { a[x] = a[x-i] ; \ std::cout<<"a ["<<x<<"]= "<< a[x] <<'\n' ; \ }}void loop3(float *a, int &i){ for (int x = 0 ; x < i ; x++ ) { a[x] = x * x * x ; a[x] += a[x]; }}int main() \{ int nx ; \ float* xpos = NULL; \ xpos = new float[nx]; \ std::cout<<"enter the value for nx " ; \ std::cin>>nx ; \ int a = nx/2 ; \ std::cout<<"a (= nx/2 )= "<< a <<'\n' ; \ loop1(xpos, nx ); \ loop2(xpos, a, nx); \ for (int x = 0 ; x < nx ; x++ ) \ { std::cout<<"xpos = "<< xpos[x] <<'\n' ; \ }/* loop3(xpos, nx ); \ */ for (int x = 0 ; x < nx ; x++ ) \ { std::cout<<"new xpos = "<< xpos[x] <<'\n' ; \ } return 0 ;}Result for Input = 5enter the value for nx 5a (= nx/2 )= 2a = 0a = 1a [2]= 0a [3]= 1a [4]= 0xpos = 0xpos = 1xpos = 0xpos = 1xpos = 0new xpos = 0new xpos = 1new xpos = 0new xpos = 1new xpos = 0--------------------------------Process exited after 4.442 seconds with return value 0Press any key to continue . . .Result for Input = 6enter the value for nx 6a (= nx/2 )= 3a = 0a = 1a = 2a [3]= 0a [4]= 1a [5]= 2xpos = 0xpos = 1xpos = 2xpos = 0xpos = 1xpos = 2new xpos = 0new xpos = 1new xpos = 2new xpos = 0new xpos = 1new xpos = 2--------------------------------Process exited after 3.427 seconds with return value 0Press any key to continue . . .Result for Input = 8<pre lang="c++">enter the value for nx 8a (= nx/2 )= 4a = 0a = 1a = 2a = 3a [4]= 0a [5]= 1a [6]=--------------------------------Process exited after 7.167 seconds with return value 3221226356Press any key to continue . . .Have you got any idea about what I did wrong ?Is there any specific name of such error?But, I can't find why that failed.What I have tried:I tried to vary the Input from one to other. But giving the message. 解决方案 int nx ; \float* xpos = NULL; \xpos = new float[nx]; \You have not set a value for nx, so your array may be any size between -2,147,483,647 and +2,147,483,647.And remove those backslashes everywhere, what do you think they are for?Look at your code:int main() \{ int nx ; \ float* xpos = NULL; \ xpos = new float[nx]; How many floats are allocated in the array? Since you don't set a value for nx it will (if you are lucky, it depends on the compiler and the settings you gave it) default to zero.And since it's a local variable, the array is on the stack. When you use it, you access parts of the stack that aren't a part of your array at all, and you corrupt them when you write to the array. At that point, anything can happen!Allocate some suitable size to the array, or allocate the array once you know how many elements it needs to have!To fix the obvious in your code#include <iostream>void loop1(float a[], size_t k ){ size_t limit = k / 2 ; for (size_t x = 0 ; x < limit ; x++ ) { a[x] = x ; std::cout << "a = " << a[x] << '\n' ; }}void loop2(float a[], size_t i, size_t j ){ for (size_t x = i ; x < j ; x++ ) { a[x] = a[x-i] ; std::cout<<"a ["<<x<<"]= "<< a[x] <<'\n' ; }}void loop3(float a[], size_t i){ for (size_t x = 0 ; x < i ; x++ ) { a[x] = x * x * x ; a[x] += a[x]; }}int main(){ int nx ; float* xpos = NULL; std::cout<<"enter the value for nx " ; std::cin>>nx ; xpos = new float[nx]; int a = nx/2 ; std::cout<<"a (= nx/2 )= "<< a <<'\n' ; loop1(xpos, nx ); loop2(xpos, a, nx); for (int x = 0 ; x < nx ; x++ ) { std::cout<<"xpos = "<< xpos[x] <<'\n' ; }/* loop3(xpos, nx ); */ for (int x = 0 ; x < nx ; x++ ) { std::cout<<"new xpos = "<< xpos[x] <<'\n' ; } delete [] xpos;}You should give meaningful names to your functions.You could use std::vector instead of dynamically allocate yourself the array. 这篇关于获得“返回值3221226356”在C ++代码中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-14 08:42