Strcat () & strcpy () works even if there is not enough memory

When implementing infix on postfix algorithms and vice versa

I found,

char * str = (char*) malloc(1);
strcpy(str,str2);

this works even if it str2contains more than 1 character (20 characters or more).

I had no warnings or errors at runtime.

How is this possible?

Note:

I also did not receive garbage.

I always get the required result regardless str2.

+3
source share
2 answers

The reason for this is that C has free memory access. Strcpy / strcat does not have NO IDEA, how much memory is on the other end of the pointer you gave them. They just assume that you know what you are doing, and go for it.

, : , . , , . , , . , ? - !

, - , , , , , , -, .

, , malloc.

, - , , ( ), strncpy. , .

+7

strcat strcpy. strncat strncpy. , strncpy strncat .

+2

All Articles