一个linux下C语言变成里的关于mmap用法的错误
1#include<stdio.h>2#include<stdlib.h>3#include<unistd.h>4#include<sys/mman.h>5intmain...
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <sys/mman.h>
5 int main(void){
6 char * psz= mmap(sbrk(0)/*NULL*/,/*256*/
7 8192,
8 PROT_READ|PROT_WRITE,
9 MAP_PRIVATE|MAP_ANONYMOUS,0,0);
10 if(psz==MAP_FAILED){
11 perror("mmap");
12 return -1;
13 }
14 printf("psz=%p\n",psz);
15 sprintf(psz,"hello memory!");
16 printf("%s",psz);
17 printf("%d\n",getpid());
18 getchar();
19 if(munmap(psz,4096)==-1){
20 perror("munmap(1)");
21 return -1;
22 }
23 getchar();
24 if(munmap(psz+4096,4096)==-1){
25 perror("munmay(2)");
26 return -1;
27 }
28 getchar();
29 char *p = malloc (1024);
30 free(p+512);
31 return 0;
32 }
错误
psz = 0x9f5f000
Hello, Memory !
2874
*** Error in `./a.out': free(): invalid pointer: 0x09f5f208 ***
已放弃 (核心已转储) 展开
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <sys/mman.h>
5 int main(void){
6 char * psz= mmap(sbrk(0)/*NULL*/,/*256*/
7 8192,
8 PROT_READ|PROT_WRITE,
9 MAP_PRIVATE|MAP_ANONYMOUS,0,0);
10 if(psz==MAP_FAILED){
11 perror("mmap");
12 return -1;
13 }
14 printf("psz=%p\n",psz);
15 sprintf(psz,"hello memory!");
16 printf("%s",psz);
17 printf("%d\n",getpid());
18 getchar();
19 if(munmap(psz,4096)==-1){
20 perror("munmap(1)");
21 return -1;
22 }
23 getchar();
24 if(munmap(psz+4096,4096)==-1){
25 perror("munmay(2)");
26 return -1;
27 }
28 getchar();
29 char *p = malloc (1024);
30 free(p+512);
31 return 0;
32 }
错误
psz = 0x9f5f000
Hello, Memory !
2874
*** Error in `./a.out': free(): invalid pointer: 0x09f5f208 ***
已放弃 (核心已转储) 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询