为什么以下编译没有错误?:
int main()
{
int x = x; //I thought this should cause an error
return 0;
}
Run Code Online (Sandbox Code Playgroud)
标准中的哪个部分解释了为什么允许这样做?
在malloc()用于分配内存空间之后,我很好奇指针究竟是什么?该联机帮助页告诉我calloc()初始化已分配的内存空间为零.
malloc()函数分配大小字节并返回指向已分配内存的指针. 内存未初始化.如果size为0,则malloc()返回NULL或一个以后可以成功传递给free()的唯一指针值.
和
calloc()函数为每个大小为字节的nmemb元素数组分配内存,并返回指向已分配内存的指针. 内存设置为零.如果nmemb或size为0,则calloc()返回NULL或一个以后可以成功传递给free()的唯一指针值.
我在C中为C(haha)创建了一个非常简短的示例程序:
int main() {
char *dynamic_chars;
unsigned amount;
printf("how much bytes you want to allocate?\n");
scanf("%d", &amount);
dynamic_chars = (char*)malloc(amount*sizeof(char));
printf("allocated:\n%s\n", dynamic_chars);
free(dynamic_chars);
return 0;
Run Code Online (Sandbox Code Playgroud)
}
但是,在执行此代码时,它只输出任何内容.如果我初始化内存,例如0xFFFF使用循环初始化每个字节,那么程序会向我显示我期望的内容.内存空间实际存在,因为我不会收到错误声称我正在尝试访问未初始化的变量.
由于内存空间通常不会被删除但被标记为可重写,我想知道如果通过执行我的程序,我是否应该能够看到以前使用的随机内存字节?但我什么都看不到,所以我真的很困惑malloc().
关于malloc()或者可能是内存使用的另一件事,关于我的程序是有趣的:如果我使用calloc(),分配内存,我可以通过例如监视来跟踪我的程序的实际内存使用情况.例如,如果我告诉我的程序,每次分配1.000.000.000字节的内存calloc()我将在我的系统监视器中看到以下内容:
c
malloc
initialization
dynamic-memory-allocation
在以下程序中,输出始终为零或未定义的行为?
#include<iostream>
int main()
{
int i= i ^ i ;
std::cout << "i = " << i << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
用gcc 4.8.0这段代码编译成功,输出为0.
为什么输出是5 0我无法理解3 ^ 6 = 5的逻辑,实际上它应该是729对吗?这是代码
#include <stdio.h>
int main()
{
int a;
printf("%d\n",(3^6));
printf("%d",(a^a));
return 0;
}
Run Code Online (Sandbox Code Playgroud) 这是一个理论问题而不是实际问题,但我想知道是否可以在C(或C++)中取消初始化变量.所以我们假设我们有以下代码:
void some_fun()
{
int a; // <-- Here a is an un-initialized variable, it's value is the value
// found in the memory at the given location.
a = 7; // Now I have initialized it
// do something with a ...
// and here do something again with a that it
// will have the same state (ie: indeterministic) as before initialization
}
Run Code Online (Sandbox Code Playgroud)
(不,我不想放一个随机值,a因为那也是一个初始化,也不是0,因为这是一个非常好的价值,...我只是希望它再次在那个"我不知道关于它的任何事情"在初始化之前进行阶段化".
(是的我知道:C中声明的,未初始化的变量会发生什么?它有值吗?)
在阅读本文时,我看到了一个我不了解的UB,希望您能澄清一下
size_t f(int x)
{
size_t a;
if(x) // either x nonzero or UB
a = 42;
return a;
}
Run Code Online (Sandbox Code Playgroud)
我猜UB是由于a没有初始化的值,但这不是它定义的行为吗?意思是,无论f(0)变量a是什么, 都将返回其所保存的值(我认为这类似于rand())。我们是否必须知道代码片段返回的值使代码具有明确定义的行为?
我在Mac OS X上使用C(版本10.6.8); 这是一个小程序,用于测试在声明int但未初始化时发生的情况:
#include <stdio.h>
int main() {
int a;
printf("\n%d\n\n", a);
}
Run Code Online (Sandbox Code Playgroud)
当我这样做时gcc -std=c99 test.c; ./a.out,我得到一个可预测的结果:unsigned int(32767)的最大值.但是,当我使用-ogcc 时会发生一些奇怪的事情.当我这样做时,gcc -std=c99 test.c -o test.out; ./test.out我得到了别的东西:0.
我发现,无论我将文件命名为什么或如何编译它都没关系.如果名字不是 a.out,那我得到0.当名称是a.out我得到最大的uint类型或一些其他更大的数字.
与下面的一些想法相反,只有当编译文件a.out非常奇怪时,这个数字才是半随机的(大多数是最大的uint,但有时候数字更大).它可能只是我的电脑; 或者,也许,我幸运地 - 或者不幸的是 - 连续30次收到相同的结果.
我用C创建了这个小软件:
#include <stdio.h>
#include <stdlib.h>
void print_this_list(int size){
int list[size];
for (int i = 0; i < size; i++) {
printf("%d\n", list[i]);
}
}
int main(int argc, char *argv[]){
print_this_list(25);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
执行结果非常有趣(显然)随机数:
-1519340623
859152199
-1231562870
-1980115833
-1061748797
1291895270
1606416552
32767
15
0
1
0
104
1
1606578608
32767
1606416304
32767
1606423158
32767
1606416336
32767
1606416336
32767
1
Program ended with exit code: 0
Run Code Online (Sandbox Code Playgroud)
究竟是什么数字以及它们背后的"逻辑"是什么?
我是一名业余C程序员,我在一本书中遇到过这个问题,有人可以给我一个有效的解释.我对这个^符号在C程序中的作用感到困惑.
#include <stdio.h>
int main(void)
{
int a;
printf("%d", (3^6) + (a^a));
return 0;
}
Run Code Online (Sandbox Code Playgroud) 在 C 中,以下代码是有效的,但在 C++ 中,它需要您对其进行初始化。
const size_t s;
Run Code Online (Sandbox Code Playgroud)
在 C 中,你必须去掉常量,然后再初始化它,但它不能保证工作。但是,如果他们允许此功能存在,那么他们一定有充分的理由。那么为什么在 C++ 中会发生这种变化呢?