简体   繁体   English

Xcode中的Strcpy

[英]Strcpy in xcode

This is my code 这是我的代码

char strings[10][10] = {"aaa", "bbb", "ccc"};
strcpy(strings[0], strings[0]);

I create a C project in Xcode, and then I run it which will be crash. 我在Xcode中创建一个C项目,然后运行它将崩溃。 But if it is a Objective-C project, it will be no problem 但是,如果这是一个Objective-C项目,那就没问题了

Calling strcpy when source and destination are overlapping (or identical as in your case) is undefined behaviour in C, C++, Objective-C and Objective-C++. 当源和目标重叠(或与您的情况相同)时调用strcpy在C,C ++,Objective-C和Objective-C ++中是未定义的行为。 Undefined behaviour means "anything can happen". 未定义的行为表示“可能发生任何事情”。 "Anything" includes crashing with one compiler and not crashing with another compiler. “任何”都包括使一个编译器崩溃而没有使另一编译器崩溃。 It also includes worse things. 它还包括更糟糕的事情。

if it is a C project, the strcpy will be replaced by #if __has_builtin(__builtin___strcpy_chk) || defined(__GNUC__) 如果是C项目,则将strcpy替换为#if __has_builtin(__builtin___strcpy_chk) || defined(__GNUC__) #if __has_builtin(__builtin___strcpy_chk) || defined(__GNUC__) #undef strcpy #define strcpy(dest, src) \\ __builtin___strcpy_chk (dest, src, __darwin_obsz (dest)) #endif #if __has_builtin(__builtin___strcpy_chk) || defined(__GNUC__) #undef strcpy #if __has_builtin(__builtin___strcpy_chk) || defined(__GNUC__) #define strcpy(dest, src) \\ __builtin___strcpy_chk (dest, src, __darwin_obsz (dest)) #endif

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

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