在 C++ 中捕获错误的数组引用

发布于 2024-07-21 18:46:30 字数 440 浏览 8 评论 0原文

如何在 C++ 中捕获错误的数组引用? 为什么以下代码不起作用:

    #include <exception>

    int * problemNum = new int;
    int (* p [100])() = {problem1, problem2, problem3};

    ...

    try {
        cout << (*p[*problemNum-1])();
    }
    catch (exception){
        cout << "No such problem";
    }

我的编译器说:在 Euler.exe 中的 0xcccccccc 处出现未处理的异常:0xC0000005:访问冲突。当我通过输入 0 启动错误引用时*问题编号。

How do I catch wrong array reference in C++? Why doesn't the following code work:

    #include <exception>

    int * problemNum = new int;
    int (* p [100])() = {problem1, problem2, problem3};

    ...

    try {
        cout << (*p[*problemNum-1])();
    }
    catch (exception){
        cout << "No such problem";
    }

My compiler says: Unhandled exception at 0xcccccccc in Euler.exe: 0xC0000005: Access violation. when I initiate bad reference by inputting 0 as *problemNum.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

狠疯拽 2024-07-28 18:46:31

因为C++无法用其异常机制来处理此类错误。
有关该问题,请参阅有缺陷的 C++

使用sigaction(2)

sigaction - 检查并更改信号操作

概要

   #include <signal.h>

   int sigaction(int signum, const struct sigaction *act,
                 struct sigaction *oldact);

描述
sigaction() 系统调用用于更改进程在收到特定消息后所采取的操作
信号。
Signum 指定信号,可以是除 SIGKILL 和 SIGSTOP 之外的任何有效信号。
如果 act 不为空,则从 act 安装信号signum 的新操作。 如果 oldact 非空,
之前的操作保存在oldact 中。
sigaction 结构定义如下:

       struct sigaction {
           void     (*sa_handler)(int);
           void     (*sa_sigaction)(int, siginfo_t *, void *);
           sigset_t   sa_mask;
           int        sa_flags;
           void     (*sa_restorer)(void);
       };

您需要捕获 SIGSEGV,您可以附加自己的处理程序(执行非法内存访问时调用的函数)。

Becauce C++ can't handle such errors with its exception mechanism.
See Defective C++ on that issue.

Use sigaction(2).

sigaction - examine and change a signal action

SYNOPSIS

   #include <signal.h>

   int sigaction(int signum, const struct sigaction *act,
                 struct sigaction *oldact);

DESCRIPTION
The sigaction() system call is used to change the action taken by a process on receipt of a specific
signal.
signum specifies the signal and can be any valid signal except SIGKILL and SIGSTOP.
If act is non-null, the new action for signal signum is installed from act. If oldact is non-null,
the previous action is saved in oldact.
The sigaction structure is defined as something like:

       struct sigaction {
           void     (*sa_handler)(int);
           void     (*sa_sigaction)(int, siginfo_t *, void *);
           sigset_t   sa_mask;
           int        sa_flags;
           void     (*sa_restorer)(void);
       };

You need to catch SIGSEGV, you can attach your own handler (function which gets called when illegal memory access is performed).

柒七 2024-07-28 18:46:30

alamar 是对的 - C++ 不会捕获这种类型数组的异常。

使用 STL 向量代替:

#include <exception>
#include <vector>

int * problemNum = new int;
std::vector<int(*)()> p;
p.push_back(problem1);
p.push_back(problem2);
p.push_back(problem3);

...

try {
    cout << p.at(*problemNum-1)();
}
catch (exception){
    cout << "No such problem";
}

alamar is right - C++ won't catch exceptions with this type of array.

Use an STL vector instead:

#include <exception>
#include <vector>

int * problemNum = new int;
std::vector<int(*)()> p;
p.push_back(problem1);
p.push_back(problem2);
p.push_back(problem3);

...

try {
    cout << p.at(*problemNum-1)();
}
catch (exception){
    cout << "No such problem";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文