根据我对C++的基本知识,我假设下面的代码会有运行时错误。由于编译器没有为y
指针分配任何空间,因此应在将值指定给y
指针之前添加y = new int;
。为指针赋值无新
我错了吗或者编译器有隐式分配空间给y
指针吗? (I编译我与开发 - C++代码4.9.9.2。)
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int* x;
int* y;
x = new int;
*x = 42;
cout << *x << "\n";
*y = 13;
cout << *y << "\n";
}
这是未定义的行为。代码仍然是越野车。 – juanchopanza
它当然没有。 –
它可能是未定义的行为,只是没有显示错误。 – Codor