#include <stdio.h>
int const volatile count = 1; //force to allocate writeable memory
int const count2 = 2; //at most allocate readonly memory
int main()
{
printf("&count = 0x%08X/n", &count);
printf("count = %d/n", count);
*(int*)(&count) = 9;
printf("count = %d/n", count);
printf("&count2 = 0x%08X/n", &count2);
printf("count2 = %d/n", count2);
*(int*)(&count2) = 10; //cause segmentation fault here
printf("count2 = %d/n", count2);
return 0;
}
本文通过一个简单的C语言程序演示了如何使用不同类型的变量,并尝试修改其值。特别是对比了常量指针与普通变量在内存分配上的区别,以及当试图修改只读内存区域的内容时会触发的错误。
692

被折叠的 条评论
为什么被折叠?



