gpt4 book ai didi

c - 实现一个新的strcpy函数重新定义库函数strcpy?

转载 作者:太空狗 更新时间:2023-10-29 17:14:35 26 4
gpt4 key购买 nike

据说我们可以写多个声明,但只能写一个定义。现在,如果我使用相同的原型(prototype)实现自己的 strcpy 函数:

char * strcpy ( char * destination, const char * source );

那我不是在重新定义现有的库函数吗?这不应该显示错误吗?或者它是否与库函数以目标代码形式提供的事实有某种关系?

编辑:在我的机器上运行以下代码会显示“段错误(核心已转储)”。我在 linux 上工作并且没有使用任何标志进行编译。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *strcpy(char *destination, const char *source);

int main(){
char *s = strcpy("a", "b");
printf("\nThe function ran successfully\n");
return 0;
}

char *strcpy(char *destination, const char *source){
printf("in duplicate function strcpy");
return "a";
}

请注意,我并不是要实现该功能。我只是想重新定义一个函数并询问结果。

编辑 2:应用 Mats 建议的更改后,程序不再出现段错误,尽管我仍在重新定义函数。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *strcpy(char *destination, const char *source);

int main(){
char *s = strcpy("a", "b");
printf("\nThe function ran successfully\n");
return 0;
}

char *strcpy(char *destination, const char *source){
printf("in duplicate function strcpy");
return "a";
}

最佳答案

C11(ISO/IEC 9899:201x) §7.1.3 保留标识符

— Each macro name in any of the following subclauses (including the future library directions) is reserved for use as specified if any of its associated headers is included; unless explicitly stated otherwise.

— All identifiers with external linkage in any of the following subclauses (including the future library directions) are always reserved for use as identifiers with external linkage.

— Each identifier with file scope listed in any of the following subclauses (including the future library directions) is reserved for use as a macro name and as an identifier with file scope in the same name space if any of its associated headers is included.

如果程序在保留标识符的上下文中声明或定义标识符,或者将保留标识符定义为宏名称,则行为未定义。请注意,这并不意味着您不能这样做,因为 this post表明,它可以在 gcc 和 glibc 中完成。

glibc §1.3.3 Reserved Names证明了一个更明确的原因:

The names of all library types, macros, variables and functions that come from the ISO C standard are reserved unconditionally; your program may not redefine these names. All other library names are reserved if your program explicitly includes the header file that defines or declares them. There are several reasons for these restrictions:

Other people reading your code could get very confused if you were using a function named exit to do something completely different from what the standard exit function does, for example. Preventing this situation helps to make your programs easier to understand and contributes to modularity and maintainability.

It avoids the possibility of a user accidentally redefining a library function that is called by other library functions. If redefinition were allowed, those other functions would not work properly.

It allows the compiler to do whatever special optimizations it pleases on calls to these functions, without the possibility that they may have been redefined by the user. Some library facilities, such as those for dealing with variadic arguments (see Variadic Functions) and non-local exits (see Non-Local Exits), actually require a considerable amount of cooperation on the part of the C compiler, and with respect to the implementation, it might be easier for the compiler to treat these as built-in parts of the language.

关于c - 实现一个新的strcpy函数重新定义库函数strcpy?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17631490/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com