C 错误检查函数

发布于 2024-09-19 01:05:43 字数 786 浏览 3 评论 0原文

在我的系统编程课程中,我们用 C 进行了大量编程,并且需要对大多数函数进行错误检查,因为我们目前正在学习使用 pthread 进行编程。

我之所以说这不是真正的家庭作业,是因为它远远超出了这门课的预期。只需单独检查每个功能就足够令人满意了。我只是觉得这是一种耗时且混乱的方法,希望有一个更简洁的解决方案。

我想知道是否有人可以向我展示如何编写一个函数,该函数将任何 C 函数作为参数,后跟该函数的所有必需参数以及所需的返回值(在本例中为纠正一个),并执行以下操作。

if(function_name(param1, param2, ...) != desired_return_value) {
    fprintf(stderr, "program_name: function_name() failed\n");
    perror("function_name(): ");
}

这可能吗?我们的课程几乎不需要这样做,但令我烦恼的是,几乎我编写的每个函数都必须有 4 行代码来进行错误检查。这使得它很难阅读。

甚至一些其他建议也会很好。我只是想增加可读性,所以如果这是完全错误的方向,那么一些正确的方向将不胜感激。

编辑: 理想情况下,这应该在 gnu99 标准下编译 :P

编辑 2:回应 James McNellis: 我们的函数中的错误不需要处理(我相信在这种情况下)。只需提供通知即可。我们没有涉及处理线程/进程相关错误(简而言之,这就是这个主题)。

For my systems programming class we're doing a lot of programming in C and are required to error check most functions as we are currently learning to program with pthreads.

The reason I say this is not really homework, is that it is far above and beyond what is expected for this class. Simply checking each function individually is more than satisfactory. I just feel this is a time-consuming and messy method and hope for a neater solution.

I was wondering if anyone could show me how to write a function that takes any C function as a parameter, followed by all the required parameters for that function, along with a desired return value (in this case the correct one), and performs the following.

if(function_name(param1, param2, ...) != desired_return_value) {
    fprintf(stderr, "program_name: function_name() failed\n");
    perror("function_name(): ");
}

Is this possible? It's hardly required by our course, but it just irks me that virtually ever function I write has to have 4 lines of code to error check it. It makes it bloody hard to read.

Even some other suggestions would be good. I'm just trying to increase readability, so if this is totally the wrong direction, some correct direction would be much appreciated.

EDIT: This should compile under the gnu99 standard ideally :P

EDIT 2: In response to James McNellis:
The errors from our functions do not (I believe in this case), need to be handled. Notification only needs to be supplied. We have covered nothing on handling thread/process related errors (which is this subject in a nutshell).

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

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

发布评论

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

评论(1

冰葑 2024-09-26 01:06:05

在不使用宏的情况下用 C 语言编写通用代码并不是最容易的事情。

对于使用可变参数宏的(非常)基本解决方案:(

#define CALL_AND_CHECK(f, r, ...)                                \
    do {                                                         \
        if (f(__VA_ARGS__) != r)                                 \
        {                                                        \
            fprintf(stderr, "program_name: " #f "() failed\n");  \
            perror(#f "(): ");                                   \
        }                                                        \
    } while (0)

请参阅为什么在 C 和 C++ 宏中有时会出现无意义的 do/while 和 if/else 语句? 为什么使用“无意义”的 do/while 循环)

请注意打印输出一条错误消息而不实际处理该错误几乎肯定是一个坏主意。通常,不同的错误需要以不同的方式处理,因此像这样的通用代码可能不是特别有用。如果您不想尝试从任何这些错误中恢复,您可以使用 exit(),这对于作业来说可能没问题,但在实际程序中您不希望这样做这样做。

Writing generic code in C without using macros isn't the easiest thing to do.

For a (very) basic solution using a variadic macro:

#define CALL_AND_CHECK(f, r, ...)                                \
    do {                                                         \
        if (f(__VA_ARGS__) != r)                                 \
        {                                                        \
            fprintf(stderr, "program_name: " #f "() failed\n");  \
            perror(#f "(): ");                                   \
        }                                                        \
    } while (0)

(See Why are there sometimes meaningless do/while and if/else statements in C and C++ macros? for why the "meaningless" do/while loop is used)

Note that printing out an error message and not actually handling the error is almost certainly a bad idea. Generally, different errors need to be handled in different ways, so generic code like this may not be particularly useful. If you don't want to try and recover from any of these errors, you could exit(), which might be okay for an assignment, though in a real-world program you wouldn't want to do that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文