简体   繁体   English

为什么这个编译没有错误?

[英]Why is this compiling withouth errors?

I'm a complete noob at C and i need some help understanding why a certain piece of code compiles correctly. 我是C的一个完整的菜鸟,我需要一些帮助来理解为什么某段代码正确编译。

main(){
    char name[3];
    strcpy(name, "12345678912312");
    printf("%s\n",name);
}

So this code compiles correctly;however, I don't understand why it does not cause a segmentation fault. 所以这段代码编译正确;但是,我不明白为什么它不会导致分段错误。 From my understanding of c, each character is 1 byte. 根据我对c的理解,每个字符都是1个字节。 The array name, is supposed to be able to hold 3 bytes, instead it can hold a lot more than that. 数组名称应该能够容纳3个字节,而不是它可以容纳更多。 Why is that? 这是为什么?

Additionally, if I add one more character to this, I will get Illegal Instruction(core dumped). 另外,如果我再添加一个字符,我将获得非法指令(核心转储)。

main(){
    char name[3];
    strcpy(name, "123456789123121");
    printf("%s\n",name);
}

Then if I add another character to that code, it will throw a Segmentation fault (core dumped) error. 然后,如果我向该代码添加另一个字符,它将抛出Segmentation fault(core dumped)错误。 Why are the errors different? 为什么错误不同? And why did they not occur before? 为什么他们之前没有出现?

And lastly, where can I find documentation for each function? 最后,我在哪里可以找到每个功能的文档? I'm coming from java so I am used to referencing to the java docs. 我来自java所以我习惯引用java文档。

Im using GCC compiler in Ubuntu linux. 我在Ubuntu linux中使用GCC编译器。

Both of code invokes undefined behavior because you are writing to an un-allocated memory location. 这两个代码都会调用未定义的行为,因为您正在写入未分配的内存位置。 In this case anything could happen. 在这种情况下,一切都可能发生 Either your program runs and may or may not give the expected output or it will crash or give segmentation fault. 您的程序运行,可能会或可能不会给出预期的输出,否则它将崩溃或给出分段错误。
Also note that strcpy doesn't check for array bound and compiler doesn't raise any warning/error for it. 另请注意, strcpy不检查数组绑定,并且编译器不会为它引发任何警告/错误。

If you read a few questions here on SO you will hear a lot about "undefined behaviour", often abbreviated to UB. 如果您在此处阅读了几个问题,您会听到很多关于“未定义行为”的内容,通常缩写为UB。

What it means is that if your program does something outside the C standards, the standards do not define what will happen. 这意味着如果您的程序执行C标准以外的某些操作,标准就不会定义会发生什么。 Anything can happen. 任何事情都可能发生。

Writing past the end of an array is one example of something that can trigger UB. 在数组末尾写入是可以触发UB的一个示例。

C does not do array bound checking, so if you try to write beyond the end of the array, the results will depend on how the compiler implements arrays, how they are arranged in memory, and what lies after them. C不进行数组绑定检查,因此如果您尝试在数组末尾写入,结果将取决于编译器如何实现数组,它们如何在内存中排列,以及它们之后的内容。 The point, however, is that you cannot rely on any particular behaviour. 然而,重点是你不能依赖任何特定的行为。

My favourite reference site for C and C++ is cppreference . 我最喜欢的C和C ++参考站点是cppreference But on Linux you can also read the definition of library functions with man, eg. 但是在Linux上你也可以用man来阅读库函数的定义,例如。 man strcpy . man strcpy

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

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