相关疑难解决方法(0)

当"while" - C中使用常量时,为什么MSVC会生成警告C4127

对于代码,

while(1)
{
   /* ..... */
}
Run Code Online (Sandbox Code Playgroud)

MSVC生成以下警告.

warning C4127: conditional expression is constant
Run Code Online (Sandbox Code Playgroud)

警告的MSDN页面建议使用for(;;)而不是while(1).我想知道它有什么优势for(;;)以及为什么它会在不断使用中发出警告while

我应该在GCC上使用什么标志来获得相同的警告?

c compiler-warnings visual-c++ c4127

9
推荐指数
2
解决办法
3114
查看次数

内存读取没有malloc

我写了一个C程序如下:

void foo(int *a) {
  if (a[1000] == a[1000]) {
    printf("Hello");
  } 
}

int main() {
  int *a;
  foo(a);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

我期待这个程序崩溃,因为我没有在&a [1000]分配内存,但程序实际上没有崩溃并打印"Hello".我用命令编译了程序

gcc -O0 foo.c
Run Code Online (Sandbox Code Playgroud)

可能是什么原因?

c memory-management compiler-optimization undefined-behavior

-3
推荐指数
2
解决办法
341
查看次数