一道内存分配的面试题
小黑格子屋 文章源自JAVA秀-https://www.javaxiu.com/30660.html
以下文章来源于嵌入式Linux,作者写代码的篮球球痴文章源自JAVA秀-https://www.javaxiu.com/30660.html
文章源自JAVA秀-https://www.javaxiu.com/30660.html 文章源自JAVA秀-https://www.javaxiu.com/30660.html 嵌入式Linux文章源自JAVA秀-https://www.javaxiu.com/30660.html 做个好人,加个晚班!文章源自JAVA秀-https://www.javaxiu.com/30660.html
文章源自JAVA秀-https://www.javaxiu.com/30660.html
来源:嵌入式Linux文章源自JAVA秀-https://www.javaxiu.com/30660.html
作者:写代码的篮球球痴文章源自JAVA秀-https://www.javaxiu.com/30660.html
文章源自JAVA秀-https://www.javaxiu.com/30660.html
这是读者在知识星球上写的面试题文章源自JAVA秀-https://www.javaxiu.com/30660.html
文章源自JAVA秀-https://www.javaxiu.com/30660.html
文章源自JAVA秀-https://www.javaxiu.com/30660.html
我之前写的文章有很完整说过这部分文章源自JAVA秀-https://www.javaxiu.com/30660.html
C语言,函数不可返回指向栈内存的指针文章源自JAVA秀-https://www.javaxiu.com/30660.html
C 语言内存分配文章源自JAVA秀-https://www.javaxiu.com/30660.html
看完上面的文章,我觉得你至少对C语言程序变量内存有一个概念了解了。文章源自JAVA秀-https://www.javaxiu.com/30660.html
然后看下这几张图文章源自JAVA秀-https://www.javaxiu.com/30660.html
文章源自JAVA秀-https://www.javaxiu.com/30660.html
文章源自JAVA秀-https://www.javaxiu.com/30660.html
文章源自JAVA秀-https://www.javaxiu.com/30660.html
我们想知道一个程序栈的起始地址,我们只需要写个测试程序可以了。文章源自JAVA秀-https://www.javaxiu.com/30660.html
#include "stdio.h"#include "stdlib.h"int main(void){ int a = 3; int *p = (int *)malloc(sizeof(int)); printf("%p\n",&a); printf("%p\n",&p); printf("%p\n",p); return 0;}
文章源自JAVA秀-https://www.javaxiu.com/30660.html我们期望是这样的文章源自JAVA秀-https://www.javaxiu.com/30660.html
文章源自JAVA秀-https://www.javaxiu.com/30660.html
实际运行是这样的,跟我们预期符合文章源自JAVA秀-https://www.javaxiu.com/30660.html
0x7ffc3f47e1cc0x7ffc3f47e1d00x562bf8677260
文章源自JAVA秀-https://www.javaxiu.com/30660.html当然,也可以通过一些Linux 命令来查看这些信息文章源自JAVA秀-https://www.javaxiu.com/30660.html
如果还有其他命令大家可以补充文章源自JAVA秀-https://www.javaxiu.com/30660.html
sizenmobjdump
文章源自JAVA秀-https://www.javaxiu.com/30660.htmlsize 可以看到每个内存段的大小文章源自JAVA秀-https://www.javaxiu.com/30660.html
weiqifa@bsp-ubuntu1804:~/c$ gcc neicunfenpei.c &&size ./a.out text data bss dec hex filename 1802 616 8 2426 97a ./a.outweiqifa@bsp-ubuntu1804:~/c$
文章源自JAVA秀-https://www.javaxiu.com/30660.htmlnm 可以看到更多的信息,包括里面的地址,还有标识符的区域,想看详细的可以看看man nm。文章源自JAVA秀-https://www.javaxiu.com/30660.html
#include "stdio.h"#include "stdlib.h"int gint = 1;int main(void){ char * pch = "1231231"; int a = 3; int *p = (int *)malloc(sizeof(int)); printf("%p\n",&a); printf("%p\n",&p); printf("%p\n",p); return 0;}
文章源自JAVA秀-https://www.javaxiu.com/30660.html查看输出文章源自JAVA秀-https://www.javaxiu.com/30660.html
weiqifa@bsp-ubuntu1804:~/c$ gcc neicunfenpei.c &&size ./a.out text data bss dec hex filename 1826 620 4 2450 992 ./a.outweiqifa@bsp-ubuntu1804:~/c$ gcc neicunfenpei.c &&nm ./a.out0000000000201014 B __bss_start0000000000201014 b completed.7698 w __cxa_finalize@@GLIBC_2.2.50000000000201000 D __data_start0000000000201000 W data_start0000000000000620 t deregister_tm_clones00000000000006b0 t __do_global_dtors_aux0000000000200db0 t __do_global_dtors_aux_fini_array_entry0000000000201008 D __dso_handle0000000000200db8 d _DYNAMIC0000000000201014 D _edata0000000000201018 B _end0000000000000814 T _fini00000000000006f0 t frame_dummy0000000000200da8 t __frame_dummy_init_array_entry0000000000000974 r __FRAME_END__0000000000201010 D gint0000000000200fa8 d _GLOBAL_OFFSET_TABLE_ w __gmon_start__0000000000000830 r __GNU_EH_FRAME_HDR0000000000000580 T _init0000000000200db0 t __init_array_end0000000000200da8 t __init_array_start0000000000000820 R _IO_stdin_used w _ITM_deregisterTMCloneTable w _ITM_registerTMCloneTable0000000000000810 T __libc_csu_fini00000000000007a0 T __libc_csu_init U __libc_start_main@@GLIBC_2.2.500000000000006fa T main U malloc@@GLIBC_2.2.5 U printf@@GLIBC_2.2.50000000000000660 t register_tm_clones U __stack_chk_fail@@GLIBC_2.400000000000005f0 T _start0000000000201018 D __TMC_END__
文章源自JAVA秀-https://www.javaxiu.com/30660.htmlojbdump 大家可以自己去看看,参数比较多文章源自JAVA秀-https://www.javaxiu.com/30660.html
来看看我们前面说的题目文章源自JAVA秀-https://www.javaxiu.com/30660.html
#include "stdio.h"char * test_function(int n){ int a = 3; char *p1 = "123"; char p2[] = "456"; printf("%p %p %p\n",&a,p1,p2); if(n == 0) return p1; return (char *)p2;}int main(void){ printf("%s\n",test_function(0)); printf("%s\n",test_function(1)); getchar(); return 0;}
文章源自JAVA秀-https://www.javaxiu.com/30660.html这个代码在gcc下是编译会出现警告,而且运行后会出现段错误,因为我们访问了一个非法内存。文章源自JAVA秀-https://www.javaxiu.com/30660.html
weiqifa@bsp-ubuntu1804:~/c$ gcc neicunfenpei.c && ./a.outneicunfenpei.c: In function ‘test_function’:neicunfenpei.c:11:12: warning: function returns address of local variable [-Wreturn-local-addr] return (char *)p2; ^~~~~~~~~~0x7fffe549a724 0x5641eaea1874 0x7fffe549a7341230x7fffe549a724 0x5641eaea1874 0x7fffe549a734Segmentation fault (core dumped)weiqifa@bsp-ubuntu1804:~/c$
文章源自JAVA秀-https://www.javaxiu.com/30660.html你以为这就完了?文章源自JAVA秀-https://www.javaxiu.com/30660.html
在devC++下,是可以正常运行的文章源自JAVA秀-https://www.javaxiu.com/30660.html
文章源自JAVA秀-https://www.javaxiu.com/30660.html
这个dev C++ 让我觉得有点意思文章源自JAVA秀-https://www.javaxiu.com/30660.html
文章源自JAVA秀-https://www.javaxiu.com/30660.html
所以在gcc 下,我们修改下代码文章源自JAVA秀-https://www.javaxiu.com/30660.html
#include "stdio.h"char * pg =NULL;char * test_function(int n){ int a = 3; char *p1 = "123"; char p2[] = "456"; pg = (char *)p2; printf("%p %p %p\n",&a,p1,p2); if(n == 0) return p1; return pg;}int main(void){ printf("%s\n",test_function(0)); printf("%s\n",test_function(1)); getchar(); return 0;}
文章源自JAVA秀-https://www.javaxiu.com/30660.html再运行文章源自JAVA秀-https://www.javaxiu.com/30660.html
文章源自JAVA秀-https://www.javaxiu.com/30660.html
没有段错误,但是第二次输出为空!文章源自JAVA秀-https://www.javaxiu.com/30660.html
所以,你下次面试遇到,知道怎么回答了吗?文章源自JAVA秀-https://www.javaxiu.com/30660.html
参考:文章源自JAVA秀-https://www.javaxiu.com/30660.html
https://www.cnblogs.com/ittinybird/p/4657245.html文章源自JAVA秀-https://www.javaxiu.com/30660.html
文章源自JAVA秀-https://www.javaxiu.com/30660.html
文章源自JAVA秀-https://www.javaxiu.com/30660.html
-End-
文章源自JAVA秀-https://www.javaxiu.com/30660.html文章源自JAVA秀-https://www.javaxiu.com/30660.html
文章源自JAVA秀-https://www.javaxiu.com/30660.html 高考了,我用Python分析出了10年内高考最难的年份文章源自JAVA秀-https://www.javaxiu.com/30660.html文章源自JAVA秀-https://www.javaxiu.com/30660.html
文章源自JAVA秀-https://www.javaxiu.com/30660.html 又来了!实现微信 “炸屎”大作战文章源自JAVA秀-https://www.javaxiu.com/30660.html文章源自JAVA秀-https://www.javaxiu.com/30660.html
文章源自JAVA秀-https://www.javaxiu.com/30660.html
Java双刃剑之Unsafe类详解文章源自JAVA秀-https://www.javaxiu.com/30660.html
可乐记得加冰,爱我就要置顶
文章源自JAVA秀-https://www.javaxiu.com/30660.html
文章源自JAVA秀-https://www.javaxiu.com/30660.html
文章源自JAVA秀-https://www.javaxiu.com/30660.html
素质三连biubiubiu~
文章源自JAVA秀-https://www.javaxiu.com/30660.html

评论