为什么C语言中用printf打印一个没有\0结束符的char []类型字符串,结果不报错?

1、问题如题目。按理说应该报错的是吧?可是我用valgrind运行都没有显示错误,请大佬帮忙解释啊~

2、代码如下:

#include <stdio.h>

int main(int argc, char *argv[])
{
    int numbers[4] = {5};
    char name[4] = {'a', 'a', 'a', 'a'};
    printf("name: %s\n", name);
}

结果输出正常:

==26627== Memcheck, a memory error detector
==26627== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==26627== Using Valgrind-3.16.1 and LibVEX; rerun with -h for copyright info
==26627== Command: ./ex9_1
==26627== 
name: aaaa
==26627== 
==26627== HEAP SUMMARY:
==26627==     in use at exit: 0 bytes in 0 blocks
==26627==   total heap usage: 1 allocs, 1 frees, 1,024 bytes allocated
==26627== 
==26627== All heap blocks were freed -- no leaks are possible
==26627== 
==26627== For lists of detected and suppressed errors, rerun with: -s
==26627== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

数组越界和内存泄漏是两回事,这个检查内存泄露检查不出来。你局部变量定义在堆栈上,用完正常释放,没有内存泄漏。
内存泄漏是指动态分配内存但是没有释放。