这是我的代码.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char buf1[100];
char buf2[100];
int main()
{
char **p = (char**)(buf1+sizeof(long));
char **q = (char**)(buf2+1);
*p = (char*)malloc(100);
*q = (char*)malloc(100);
strcpy(*p, "xxxx");
strcpy(*q, "zzzz");
printf("p:%s q:%s\n", *p, *q);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我使用gcc编译代码,并像这样运行valgrind-3.6.1
valgrind --leak-check=full --log-file=test.log --show-reachable=yes ~/a.out
Run Code Online (Sandbox Code Playgroud)
valgrind给了我下面的日志
==20768== Memcheck, a memory error detector
==20768== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==20768== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
==20768== Command: /home/zxin11/a.out
==20768== Parent …Run Code Online (Sandbox Code Playgroud)