C和C++中未定义,未指定和实现定义的行为有什么区别?
c c++ undefined-behavior unspecified-behavior implementation-defined-behavior
我有以下代码片段:
class ABC{
public:
int a;
void print(){cout<<"hello"<<endl;}
};
int main(){
ABC *ptr = NULL:
ptr->print();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它运行成功.有人可以解释一下吗?
为什么这样做?
#include <iostream>
using namespace std;
int main() {
float* tab[3];
int i = 0;
while(i < 3) {
tab[i] = new float[3-i];
i++;
}
cout << tab[2][7] << endl;
tab[2][7] = 6.87;
cout << tab[2][7] << endl;
i = 0;
while(i < 3)
delete[] tab[i];
}
Run Code Online (Sandbox Code Playgroud)
虽然这个没有?
#include <iostream>
using namespace std;
int main() {
float* tab = new float[3];
cout << tab[7] << endl;
tab[7] = 6.87;
cout << tab[7] << endl;
delete[] tab;
}
Run Code Online (Sandbox Code Playgroud)
我在Win XP上使用MS VS …