相关疑难解决方法(0)

功能优化为'gcc -O2'的无限循环

背景
我被一位朋友问过以下谜题:

void fn(void)
{
  /* write something after this comment so that the program output is 10 */
  /* write something before this comment */
}

int main()
{
  int i = 5;
  fn();
  printf("%d\n", i);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

我知道可以有多个解决方案,一些涉及宏,一些假设有关实现和违反C.

我感兴趣的一个特定解决方案是对堆栈做出某些假设并编写以下代码:(我知道它是未定义的行为,但可能在许多实现中按预期工作)

void fn(void)
{
  /* write something after this comment so that the program output is 10 */
  int a[1] = {0};
  int j = 0;
  while(a[j] != 5) ++j;  /* Search stack until you find 5 */
  a[j] …
Run Code Online (Sandbox Code Playgroud)

c optimization gcc undefined-behavior

34
推荐指数
3
解决办法
3203
查看次数

是否可以访问自己大小的数组元素?

我们可以访问第m 数组元素,如果它有m个元素吗?我的意思是,如果数组有7个元素,是否有可能在数组[7]中存储任何值?但是当长度为7时,数组索引从0开始并以6结束.

c arrays

1
推荐指数
1
解决办法
106
查看次数

标签 统计

c ×2

arrays ×1

gcc ×1

optimization ×1

undefined-behavior ×1