Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.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++为堆中的指针分配内存,函数退出时不会自动释放。但是在下面的代码运行之后,我发现指针a是空的,即使它在类成员函数中被分配了一些空间 #include "string" #include <iostream> using namespace std; class Test { public: void test(int *a) { if (a == 0) { a = new int[10]; bool a_is_null = (a == 0); cout << "in class member function, after allocated, a is null or not?:" << a_is_null << endl; } }; }; int main() { int *a = 0; bool a_is_null = (a == 0); cout << "in main function, before allocated, a is null or not?:" << a_is_null << endl; Test t; t.test(a); a_is_null = (a == 0); cout << "in main function, after allocated, a is null or not?:" << a_is_null << endl; delete[] a; cin; } #包括“字符串” #包括 使用名称空间std; 课堂测试 { 公众: 无效测试(int*a){ 如果(a==0) { a=新整数[10]; bool a_是_null=(a==0); cout_C++_Pointers_Memory Management_Heap - Fatal编程技术网

为什么类成员函数破坏为指针参数分配的内存? 我知道,C++为堆中的指针分配内存,函数退出时不会自动释放。但是在下面的代码运行之后,我发现指针a是空的,即使它在类成员函数中被分配了一些空间 #include "string" #include <iostream> using namespace std; class Test { public: void test(int *a) { if (a == 0) { a = new int[10]; bool a_is_null = (a == 0); cout << "in class member function, after allocated, a is null or not?:" << a_is_null << endl; } }; }; int main() { int *a = 0; bool a_is_null = (a == 0); cout << "in main function, before allocated, a is null or not?:" << a_is_null << endl; Test t; t.test(a); a_is_null = (a == 0); cout << "in main function, after allocated, a is null or not?:" << a_is_null << endl; delete[] a; cin; } #包括“字符串” #包括 使用名称空间std; 课堂测试 { 公众: 无效测试(int*a){ 如果(a==0) { a=新整数[10]; bool a_是_null=(a==0); cout

为什么类成员函数破坏为指针参数分配的内存? 我知道,C++为堆中的指针分配内存,函数退出时不会自动释放。但是在下面的代码运行之后,我发现指针a是空的,即使它在类成员函数中被分配了一些空间 #include "string" #include <iostream> using namespace std; class Test { public: void test(int *a) { if (a == 0) { a = new int[10]; bool a_is_null = (a == 0); cout << "in class member function, after allocated, a is null or not?:" << a_is_null << endl; } }; }; int main() { int *a = 0; bool a_is_null = (a == 0); cout << "in main function, before allocated, a is null or not?:" << a_is_null << endl; Test t; t.test(a); a_is_null = (a == 0); cout << "in main function, after allocated, a is null or not?:" << a_is_null << endl; delete[] a; cin; } #包括“字符串” #包括 使用名称空间std; 课堂测试 { 公众: 无效测试(int*a){ 如果(a==0) { a=新整数[10]; bool a_是_null=(a==0); cout,c++,pointers,memory-management,heap,C++,Pointers,Memory Management,Heap,指针类似于任何其他变量,您在行中通过值传递它 t.test(a); 因此,在函数退出后指针不会被修改。通过引用传递它,您将看到差异,即声明 void Test::test(int* &a) { ...} 是的。根据您的建议,情况发生了变化。谢谢。“c++为堆中的指针分配内存”-不,您从哪里得到错误信息的?是的。您是对的。应该是运算符“new”分配的内存在堆中

指针类似于任何其他变量,您在行中通过值传递它

t.test(a);
因此,在函数退出后指针不会被修改。通过引用传递它,您将看到差异,即声明

void Test::test(int* &a) { ...}

是的。根据您的建议,情况发生了变化。谢谢。“c++为堆中的指针分配内存”-不,您从哪里得到错误信息的?是的。您是对的。应该是运算符“new”分配的内存在堆中