#include <stdio.h>
 #include <stdlib.h>
 int main()
 {
     char *name;
     char *command;
     name=(char *)malloc(10);
     command=(char *)malloc(128);
     printf("address of name is : %d\n",name);
     printf("address of command is:%d\n",command);
     printf("Distance between addresses is :%d\n",command-name);
     printf("Enter your name:");
     gets(name);
     printf("Hello %s\n",name);
     system(command);
 }
分配恒定数量的内存(缓冲区大小)和两个地址之间的距离(相邻的内存块)有什么区别?在此示例中,名称和命令之间的差异为 16 字节,名称的缓冲区大小为 10 字节。哪一个会触发缓冲区溢出?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// typedef size_t u_long;
int main(){
    u_long distance;
    char *buf1= (char *)malloc(16);
    char *buf2= (char *)malloc(16);
    distance= (u_long)buf2 - (u_long)buf1;
    printf("buf1 = %p\nbuf2 = %p\ndistance = 0x%x bytes\n",
           buf1, buf2, distance);
    memset(buf2, 'A', 15); buf2[15]='\0';
    printf("before overflow buf2 = %s\n", buf2);
    memset(buf1, 'B', (8+distance));
    printf("after overflow buf2 = %s\n", buf2);
    return 0;
}