前言:昨天做了一个笔试题,里面有一道关于内存探究的题,之前也看过,不过没用机器实践过,有点类似只知晓理论的意味了,今日上午闲暇无事,于是去探究了一下内存在机器里面的实际情况是那样,话不多说,开摆
原题1:
void GetMemory(char *p) {p = (char *)malloc(100); } void Test(void) {char *str = NULL;GetMemory(str);strcpy(str, "hello world");printf(str); }
转换成可运行代码:
#include
#include #include void GetMemory(char *p) {p = (char *)malloc(100); } int main(void) {char *str = NULL;GetMemory(str);strcpy(str, "hello world");printf("str:%s\n",str);return 0; } 32位机:
liu@ubuntu:~/桌面$ ./a.out Segmentation fault (core dumped)
64位机:
hqyj@ubuntu:~/桌面$ ./a.out 段错误 (核心已转储)
由此看出:该错误是不可能优化的错误,也是不可轻易犯下的错误,
因为是将 str 这个地址传过去的,而不是用二级指针亦或者返回值类型等等,导致局部函数的malloc是在局部变量的基础上malloc的,修改了对真实空间无任何影响,所以在 strcpy 时会导致内存越界、段错误,从而导致程序崩溃
综上:32位机程序崩溃、64位机程序崩溃
原题2:
char *GetMemory(void) {char p[] = "hello world";return p; } void Test(void) {char *str = NULL;str = GetMemory();printf(str); }
转换成可运行代码:
#include
char *GetMemory(void) {char p[] = "hello world";return p; } int main(void) {char *str = NULL;str = GetMemory();printf("str:%s\n",str);return 0; } 32位机:
liu@ubuntu:~/桌面$ ./a.out str: �n�������NY���yr���n����������n�%�W�pRq� liu@ubuntu:~/桌面$ ./a.out str: �u���t����^`����y���u�����P�����u�%�^�pbx� liu@ubuntu:~/桌面$ cat 1.c
64位机:
hqyj@ubuntu:~/桌面$ gcc 1.c 1.c: In function ‘GetMemory’: 1.c:11:9: warning: function returns address of local variable [-Wreturn-local-addr]11 | return p;| ^ hqyj@ubuntu:~/桌面$ ./a.out str:(null) hqyj@ubuntu:~/桌面$ ./a.out str:(null)
由此可以看出:64位机在32位机的基础上做了一些优化,虽然 p是开辟的“栈内存”,函数结束后开辟的”栈内存“自动销毁,只返回一个“栈指针”,又由于没有将该指针指向NULL,所以该指针指向的东西为止,导致乱码(也就是32位机上面的结果),但是64位机的操作系统显然对此进行了一定的优化,自动将其指向了NULL(有点类似C++的智能指针的意味了)
综上:32位机乱码、64位机为NULL
原题3:
void GetMemory2(char **p, int num) {*p = (char *)malloc(num); } void Test(void) {char *str = NULL;GetMemory(&str, 100);strcpy(str, "hello");printf(str); }
转换成可运行的代码:
#include
#include #include void GetMemory(char **p, int num) {*p = (char *)malloc(num); } int main(void) {char *str = NULL;GetMemory(&str,100);strcpy(str,"hello");printf("str:%s\n",str);return 0; } 32位机:
liu@ubuntu:~/桌面$ ./a.out str:hello liu@ubuntu:~/桌面$ ./a.out str:hello
64位机:
hqyj@ubuntu:~/桌面$ ./a.out str:hello hqyj@ubuntu:~/桌面$ ./a.out str:hello
由此可以看出:由于参数列表传的是2级指针,所以在局部空间malloc过后会真正操作传进来的那个地址,所以32位机和64位机都能打印出 hello ,但是不知有没有细心的读者发现,其实malloc过后并没有free,造成了内存泄漏,工程小还没啥影响,一旦工程大,程序会越来越卡顿
综上:32位机打印hello,内存泄漏,64位机打印hello,内存泄漏
原题4:
void Test(void) {char *str = (char *) malloc(100);strcpy(str, “hello”);free(str);if(str != NULL){strcpy(str, “world”);printf(str);} }
转换成可运行的程序:
#include
#include #include int main(int argc, char *argv[]) {char *str = (char *) malloc(100);strcpy(str, "hello");free(str);if(str != NULL){strcpy(str,"world");printf("str:%s\n",str);}return 0; } 32位机:
liu@ubuntu:~/桌面$ ./a.out str:world liu@ubuntu:~/桌面$ ./a.out str:world liu@ubuntu:~/桌面$ ./a.out str:world
64位机:
hqyj@ubuntu:~/桌面$ ./a.out str:world hqyj@ubuntu:~/桌面$ ./a.out str:world hqyj@ubuntu:~/桌面$ ./a.out str:world
对于著名的野指针问题,我们可以从运行结果中得到一些东西
1)前3步没问题,free掉了str指针,但是并没有将 str指向 NULL,原有的空间的内容还在,所以可以进入if语句里面
2)对于32位和64位来说,操作系统可能对此进行了一定的优化,设想将这些指令转换成汇编,当一个中断来临,cpu第一步做到便是备份操作,由此我们是不是可以设想出,free(str)这步操作做了我们不知道的某些类似备份的操作,导致下一次再用时还能拿来用,导致world覆盖了hello,所以都打印出了world
综上:64位机打印world,32位机打印world,但是这是非法的操作,禁止这样干
这便是我对4道内存题的个人理解,如有错误,望批评指正,谢谢 !!!
参考文档:
有关内存的思考题 - YOUNG++ - 博客园 (cnblogs.com)
GetMemory错误讲解(指针练习)_xiven的博客-CSDN博客