使用GCC C在AIX上处理空指针 [英] Handling null pointers on AIX with GCC C

查看:217
本文介绍了使用GCC C在AIX上处理空指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个用C编写的代码,它有时不能很好地处理零指针。



代码最初是在Solaris上编写的,这样的指针会导致段错误。



我们的经验是,如果您从AIX上的空指针读取,您会得到0.如果您使用 xlc 你可以添加一个选项 -qcheck = all 来捕获这些指针。但是我们使用gcc(并且想要继续使用该编译器)。 gcc 是否提供这样的选项?

解决方案

gcc是否提供这样的选项?



我很害羞地自愿回答不, 。尽管我不能引用关于gcc和运行时NULL检查的信息。



你正在处理的问题是你试图在一个写得不好的程序中定义一些未定义的行为。



我建议你咬紧牙关,切换到xlc或手动添加 NULL 检查代码,直到找到并删除不良行为。



考虑:


  • 制作一个宏来检查一个指针

    li>
  • 在指针赋值后添加该宏

  • 将该宏添加到接受指针的函数的入口点



如果删除了错误,您可以开始删除这些检查。


We have a code written in C that sometimes doesn’t handle zero pointers very well.

The code was originally written on Solaris and such pointers cause a segmentation fault. Not ideal but better than ploughing on.

Our experience is that if you read from a null pointer on AIX you get 0. If you use the xlc compiler you can add an option -qcheck=all to trap these pointers. But we use gcc (and want to continue using that compiler). Does gcc provide such an option?

解决方案

Does gcc provide such an option?

I'm sheepishly volunteering the answer no, it doesn't. Although I can't cite the absence of information regarding gcc and runtime NULL checks.

The problem you're tackling is that you're trying to make undefined behavior a little more defined in a program that's poorly-written.

I recommend that you bite the bullet and either switch to xlc or manually add NULL checks to the code until the bad behavior has been found and removed.

Consider:

  • Making a macro to null-check a pointer
  • Adding that macro after pointer assignments
  • Adding that macro to the entry point of functions that accept pointers

As bugs are removed, you can begin to remove these checks.

这篇关于使用GCC C在AIX上处理空指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆