diadia said:
#include <iostream>
using namespace std;
void main()
{
char *a;
a = new char[1];
cin >> a;
cout << a;
}
I don't know why this code can work and don't occur segmentation
error?
You mean, when you enter something?
Well. Technically you write beyond the allocated memory. What happens
in such a case is 'undefined behaviour', meaning: anything can happen.
A segmentation error or access violation is just one possibility among
many others and hopefully you have learned by this example, that the
absence of an access violation (which usually is detected by the operating
system and not by the program runtime system) doesn't mean that your
program is correct. The program you wrote still has a problem but
in this specific case it went by unnoticed by the programmer. In other
cases or when you start altering the program and add things to it the error
may manifest itself in strange results which seem to be completely illogical
until you figure out that writing beyond allocation has happend.