a.h:
#include <iostream>
int *a1 = new int[5];
using namespace std;
class AA
{
public:
AA();
~AA();
void work();
static void signalHandler(int signum);
private:
};
AA::AA()
{
}
AA::~AA()
{
}
a.cpp:
#include "a.h"
#include <cstdlib>
#include <signal.h>
#include <unistd.h>
void AA::work() {
// 注册信号处理程序
signal(SIGINT, signalHandler);
signal(SIGSEGV, signalHandler);
signal(SIGTERM, signalHandler);
int c = 0;
while (true)
{
a1[c] = c + 1;
}
//后面的代码不会执行
cout << 111 << endl;
}
void AA::signalHandler(int signum)
{
delete[] a1;cout<<"delete a1"<<endl;
// 退出程序
std::exit(signum);
}
int main() {
AA a;
a.work();
}
运行截图: