3

我如何确保如果有人在代码中使用特定函数(比如 memcpy),那么它会返回错误。

我们已经使用一些内部设计的函数从代码中删除了所有 memcpy 实例,

我要确保的是,将来每当有人使用 memcpy 时,编译器都会抛出错误消息。

4

2 回答 2

5

You can use the preprocessor for this, like

#define memcpy(a, b, c) do_not_use_memcpy

Put that in a header file that is included in all source files, and the preprocessor will replace all calls to memcpy with the (undefined) symbol do_not_use_memcpy. As that symbol is undefined, you will get a compiler error about it.

于 2013-07-11T11:06:41.060 回答
4

To avoid breaking libraries, use the deprecated attribute :

void * my_new_memcpy ( void * destination, const void * source, size_t num )
{
    return memcpy(destination, source, num);
} __attribute__((deprecated));

// Make sure this is used *after* declaring the function
#define memcpy my_new_memcpy
于 2013-07-11T11:06:46.580 回答