指针传递内存深入探讨(三)
来源:互联网 发布:英语辅导软件 编辑:程序博客网 时间:2024/04/29 00:31
让我们继续前两次的讨论
我们同样可以使用把指针作为返回值的方法来传递内存,而且我个人认为这是值得推荐的方式
c代码:
char *GetMemory3(int num)
{
char *p = (char *)malloc(sizeof(char)*num);
return p;
}
int main()
{
char *str = NULL;
str = GetMemory3(100);
strcpy(str, "hello");
printf("%s", str);
free(str);
}
查看汇编结果如下:
GetMemory3函数:
.file "getmemory3.c"
.text
.align 2
.globl GetMemory3
.type GetMemory3,@function
GetMemory3:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
subl $12, %esp
pushl 8(%ebp)
call malloc
addl $16, %esp
movl %eax, -4(%ebp)
movl -4(%ebp), %eax
leave
ret
.Lfe1:
.size GetMemory3,.Lfe1-GetMemory3
.section .rodata
main函数:
.LC0:
.string "hello"
.LC1:
.string "%s"
.text
.align 2
.globl main
.type main,@function
main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
movl $0, %eax
subl %eax, %esp
movl $0, -4(%ebp)
subl $12, %esp
pushl $100
call GetMemory3
addl $16, %esp
movl %eax, -4(%ebp)
subl $8, %esp
pushl $.LC0
pushl -4(%ebp)
call strcpy
addl $16, %esp
subl $8, %esp
pushl -4(%ebp)
pushl $.LC1
call printf
addl $16, %esp
subl $12, %esp
pushl -4(%ebp)
call free
addl $16, %esp
leave
ret
.Lfe2:
.size main,.Lfe2-main
.ident "GCC: (GNU) 3.2 20020903 (Red Hat Linux 8.0 3.2-7)"
- 指针传递内存深入探讨(三)
- 指针传递内存深入探讨(一)
- 指针传递内存深入探讨(二)
- 深入探讨this指针
- 深入探讨this指针
- 深入探讨this指针
- 指针深入探讨
- 深入探讨this指针
- 深入探讨this指针
- 深入探讨this指针
- 深入探讨this指针
- 深入探讨this指针
- 深入探讨this指针
- 内存分配深入探讨
- 内存分配深入探讨
- 深入探讨this指针(zz)
- 指针数组的深入探讨
- 指针如何传递内存
- GNU交叉编译环境的建立
- 同一空间绑定多个域名而实现访问不同的页面代码,PHP,jsp和ASP代码
- 求一个网上投票系统的JSP java 文件
- 员工跳槽,你怎么办?
- 游标的使用
- 指针传递内存深入探讨(三)
- 如何编写高质量的程序 文件的结构管理
- Jetty介绍
- 可分级的视频编码基础
- 程序员调试能力和相关书籍
- 人生的25句经典!
- 《蝴蝶花》
- 燃烧的远征java(三)-Struts+Spring+Hibernate:java的几种对象(PO,VO,DAO,BO,POJO)解释
- 贝叶斯