简体   繁体   English

为什么在使用argc和argv时不会出现分段错误?

[英]Why I don't get segmentation fault when using argc and argv?

I have an OpenGL program with my main with 2 parameters I am compiling with xCode on macOS 我有一个带有两个主要参数的OpenGL程序,我正在macOS上使用xCode进行编译

int main (int argc, char **argv)
{
...
glutInit(&argc, argv);

I studied that if i don't compile with a.out and 2 values after I should get segmentation fault but I'm not getting it compiling without those values. 我研究了如果在出现分段错误后不使用a.out和2值进行编译,但没有这些值就无法编译它。

The segmentation fault to which you are alluding is a particular manifestation of program undefined behaviour . 您提到的分段错误是程序未定义行为的一种特殊表现。

But undefined behaviour is exactly that: you don't know what's going to happen. 但是未定义的行为正是这样:您不知道会发生什么。

Which is why you need to check the value of argc before reading any of argv . 这就是为什么在读取任何argv之前需要检查argc的值的原因。

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

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