我有以下代码.
#include <iostream>
int * foo()
{
    int a = 5;
    return &a;
}
int main()
{
    int* p = foo();
    std::cout << *p;
    *p = 8;
    std::cout << *p;
}
而代码只是运行而没有运行时异常!
输出是 58
怎么会这样?本地变量的内存不能在其功能之外无法访问吗?
将方法与对象指针绑定并在之后删除该对象,该方法仍然是可调用的.
struct A {
    void b(){std::cout << "SUHDU" << std::endl;}
};
int main(int argc, const char * argv[])
{
    A *a = new A;
    auto f1 = std::bind(&A::b, a);
    auto f2 = std::bind(&A::b, std::ref(a));
    f1();
    f2();
    delete a;
    f1(); // still outputs SUHDU
    f2(); // still outputs SUHDU
}
为什么std::bind这样做,它是如何做到的?
最近我有c_str()的问题.下面是示例代码片段
#include<bits/stdc++.h>
#include<unistd.h>
using namespace std;
class har{
    public:
    string h;
    har(string str){
        h=str;
    }
};
int main(){
har *hg=new har("harish");
const char *ptr=hg->h.c_str();
delete hg;
cout<<ptr<<endl;
return 0;
}
我得到输出"harish"....我已经销毁了对象,但我仍然得到输出..是c_str()再次在堆中分配内存.