简体   繁体   English

如何使用C宏(#define)更改调用,但不更改原型

[英]How to use C macro's (#define) to alter calls but not prototypes

Older code in our application contains calls to malloc , realloc and free . 我们应用程序中的旧代码包含对mallocreallocfree调用。 With our updated code, our own implementations are called instead of the standard runtime ones. 使用我们更新的代码,将调用我们自己的实现,而不是标准的运行时实现。 Examples are shown below, 示例如下所示,

#define malloc(s) OurMalloc(s)
#define free(p)   OurFree(p)

This works fine for the updated code and for the newer C++ code we simply implement global new and delete operators, so the C++ solution is 'cleaner'. 这对于更新的代码和较新的C ++代码都很好,我们只需实现全局newdelete运算符,因此C ++解决方案更加“干净”。

The problem is that we now have to include a 3rd party library, which has classes that contain methods that have names like malloc and free , eg 问题在于,我们现在必须包括一个第三方库,该库具有包含类的方法,这些方法的名称类似于mallocfree ,例如

   class ABC
   {
      public:
      ...
      void free (char *p);
   };

If the free method of the class has the same number of arguments, the C/C++ preprocessor simply replaces all occurrences of free by ourFree , even in the class definition, even when calling the method free of the class ABC . 如果该类的free方法具有相同数量的参数,则C / C ++预处理器甚至即使在类定义中,甚至在调用没有ABC类的方法时,都用ourFree替换所有出现的free So the class definition above and the following call: 所以上面和下面的类定义调用:

ABC abc;
abc.free(p);

are replaced by, 被取代,

class ABC
   {
   public:
      ...
      void OurFree (char *p);
   };

ABC abc;
abc.OurFree(p);

Which may compile, but which doesn't link of course. 哪个可以编译,但是当然不链接。

If ABC::free has a different number of arguments than the standard free, the compiler still gives a warning. 如果ABC::free的参数数量与标准free的参数数量不同,则编译器仍会发出警告。 we would like to avoid them. 我们想避免它们。

Some alternative solutions are: 一些替代解决方案是:

  • undefining our defines in the beginning of the 3rd party include file and redefining it later 在第3方开始时取消定义我们的定义,包括include文件,稍后再重新定义
  • make sure that the 3rd party include file is always included before our own define's 确保在我们自己定义的内容之前始终包含第3方包含文件

But even then, if our code is required to call these malloc or free methods of the 3rd party classes, the preprocessor will still alter the calls, unless we write all calls like this: 但是即使这样,如果需要我们的代码来调用第三方类的这些malloc或free方法,则预处理器仍会更改这些调用,除非我们编写所有如下调用:

(abc::free)(p)

Is there a way to tell a C/C++ preprocessor define that? 有没有办法告诉C / C ++预处理器来定义它?

  • only pure C-calls must be replaced 只有纯C呼叫必须被替换
  • prototypes MUST NOT be replaced 原型不得更换
  • methods in classes MUST NOT be replaced 类中的方法不得替换

How about only defining those replacements for C and not C++: 仅定义C而不是C ++的那些替换如何:

#ifndef __cplusplus
#  define malloc(s) OurMalloc(s)
#  define free(p)   OurFree(p)
#endif

Why don't you just define your own malloc and free functions instead of using macros. 为什么不只定义自己的malloc和free函数,而不使用宏。

So, change: 因此,更改:

void *OutMalloc (size_t s)

to: 至:

void *malloc (size_t s)

in the same way that you can overright operator new and delete. 就像您可以使运算符new和delete完全覆盖一样。

I think, although I may be wrong, the linker will look in object files for symbols before looking in libraries. 我认为,尽管我可能是错的,但链接器将在查找库之前先在目标文件中查找符号。

Preprocessor know nothing about about scope and semantic. 预处理器对范围和语义一无所知。 So short answer - no, you can't do that. 答案很短-不,您不能这样做。

But, you can use #undef free in library modules. 但是,您可以在库模块中#undef free使用#undef free On other side - this will not help, if you call methods abc.free() from your code. 另一方面,如果您从代码中调用方法abc.free() ,这将abc.free()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM