我正在阅读linux上的标准输入。我提供读取的缓冲区长度不足(只有两个字符),缓冲区应该溢出并且应该发生分段错误。但是程序运行正常。为什么?
编译:
gcc file.c -ansi
运行:
echo abcd | ./a.out
程序:
#include<stdio.h>
#define STDIN 0
int main() {
/* This buffer is intentionally too small for input */
char * smallBuffer = (char *) malloc( sizeof(char) * 2 );
int readedBytes;
readedBytes = read(STDIN, smallBuffer, sizeof(char) * 4);
printf("Readed: %i, String:'%s'\n", readedBytes, smallBuffer);
return 0;
}
输出:
Readed: 4, String:'abcd'