我有这样的代码:
#include <stdlib.h>
#include <stdio.h>
void func(int **b)
{
printf("b = %p\n", b); // 0x7ffe76932330
*b = *b + 1;
}
int main(void)
{
int b[10] = {0};
printf("b = %p\n", &b[0]); // 0x7ffe76932330
printf("%d\n", b[0]); // 0
func(&b);
printf("%d\n", b[0]); // 4
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这个代码有UB吗?对我来说似乎是这样,至少由于没有显式强制转换的不同类型int (*)[10] != int **。
另外,如果我有呢char b[] = "some string";?行为几乎相同......奇怪。
c pointers undefined-behavior language-lawyer implicit-conversion
我知道有很多这样的问题,但是这个问题不是关于从C标准的角度来看staticand意味着什么。volatile我对装配级别较低的情况感兴趣。
static变量的关键字使这些变量静态可见(静态存储持续时间),就像全局变量一样。为了使其成为现实,编译器应该将这些变量写入.bss节或其他地方?另外,static关键字防止变量/函数在文件外使用,它是只在编译期间发生还是有一些运行时检查?
volatile变量的关键字使这些变量从内存中读取,以确保如果其他东西(如外围设备)想要修改该变量,它将准确地看到该内存中的值。这里,“从内存中读取”到底是什么意思?使用什么内存位置?.bss,.data, 或者是其他东西?