我已经使用mmap分配了代码,但由于分段错误而无法释放它.我已经完成了mprotect – PROT_WRITE以使其可写,但我仍然无法释放它.
请帮我.
enter code here
1 #include dio.h>
2 #include Failed");
19 }
20
21 allocation = (caddr_t) mmap(0,5000,PROT_READ|PROT_NONE,MAP_PRIVATE,devZerofd,0);
22
23 if ( allocation == (caddr_t)-1 )
24 fprintf(stderr,"mmap() Failed ");
25
26 if ( mprotect((caddr_t)allocation,PROT_WRITE) < 0 )
27 fprintf(stderr,"mprotect Failed");
28 else
29 printf("mprotect done: memory allocated at address %u\n",allocation);
30
31 strcpy(allocation,"Hello,how are you");
32 puts(allocation);
33
34 if ( mprotect((caddr_t)allocation,PROT_WRITE) < 0 )
35 fprintf(stderr,"mprotect Failed");
36
37 free(allocation);
38
39 }
40
41
最佳答案
你需要使用
原文链接:https://www.f2er.com/linux/440568.htmlmunmap
.您不需要做任何其他事情(更改保护位等).但你应该检查munmap的返回码.
munmap(allocation,5000);
free(3)只能用于释放通过malloc(3)分配的内存.