snpri.c 剖析示例及打印值如下:
#include "string.h"
#include "stdio.h"
#include "stdlib.h"
void main()
{
char *p ,*q;
int i ,ioffset;
p = malloc(64);
for (i = 0; i < 10 ; i++)
{
ioffset += snprintf(p + ioffset, 64 - ioffset,"*********");
q = p + ioffset - 9;
printf("%d %d %s %s\n",i, ioffset ,p,q);
}
free(p);
}
[root@localhost snpri]# gcc -g -o snpri snpri.c ;./snpri
0 9 ********* *********
1 18 ****************** *********
2 27 *************************** *********
3 36 ************************************ *********
4 45 ********************************************* *********
5 54 ****************************************************** *********
6 63 *************************************************************** *********
7 72 ***************************************************************
8 81 *************************************************************** *********
9 90 *************************************************************** *********
*** Error in `./snpri': free(): invalid next size (fast): 0x0000000000984010 ***
======= Backtrace: =========
/lib64/libc.so.6[0x30db87d0b8]
./snpri[0x4006aa]
/lib64/libc.so.6(__libc_start_main+0xf5)[0x30db821b45]
./snpri[0x400549]