存在堆内存中的值去哪里了呢?(注释中红色字体标注)
程序代码:
#include"stdafx.h" #include<iostream> using namespace std; int* newPointerGet(int* p1) { int k1 = 55; p1 = new int; *p1 = k1; return p1; } int* PointerGet(int* p2) { int k2 = 55; p2 = &k2; return p2; } int main() { cout << "输出函数各自返回指针所指向的内存的值" << endl; int* p = NULL; p = newPointerGet(p); int* i = NULL; i = PointerGet(i); cout << "newGet:" << *p<<endl ; cout << "Get:" << *i << endl;//*i 的值不是55,显示为:260090768,每次编译显示的值还不一样。 cout << "i 所指向的内存没有被立刻销毁,执行一个输出语句后:" << endl; cout << "newGet:" << *p << endl; cout << "get:" << *i << endl; delete p; cout << "销毁内存后:" << endl; cout << "newGet:" << *p << endl; return 0; }