【发布时间】:2026-01-05 09:25:02
【问题描述】:
所以我正在学习指针并且很难确定这里的内存泄漏。我承认我以前从未使用过 malloc() 并且是指针算术的新手。提前致谢。
/*filename: p3.c */
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *buffer;
char *p;
int n;
/* allocate 10 bytes */
buffer = (char *) malloc(10);
p = buffer;
for (n=0; n<=10; n++)
*p++ = '*';
p = buffer;
for (n=0; n <=10; n++)
printf("%c ", *p++);
return 0;
}
【问题讨论】:
-
你写的越界了,你只有10个
chars的空间,但是写11个。
标签: pointers memory-leaks pointer-arithmetic