I have run the below program:
#include<stdio.h>
#include<string.h>
#include<malloc.h>
int main()
{
char *p, *q;
p = (char*)malloc(1);
q = (char*)malloc(25);
strcpy(p, "abcd");
strcpy(q,"efgh");
strcat(p,q);
printf("%s",p);
return 0;
}
I was expecting that it will give error "segmentation fault". But instead it printed the output as:
abcdefgh
I don't know how does it work because p is assigned only 1 byte and we are copying a string which takes more space. Is it the right behavior?
OS: windows 7
Compiler: mingw C compiler