数组和指针的区别
2022/2/20 6:28:16
本文主要是介绍数组和指针的区别,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
// test.c void test() { char array[4] = {0}; *(array + 0) = 0x00; array[1] = 0x11; *(array + 2) = 0x22; array[3] = 0x33; char *pointer = array; pointer[0] = 0x00; pointer[1] = 0x11; pointer[2] = 0x22; pointer[3] = 0x33; }
// Makefile test.o: test.c gcc -c -O0 -m32 -masm=intel -std=c99 -ggdb3 -fno-stack-protector -fcf-protection=none -mmanual-endbr -fno-PIE -o test.o test.c dump32: test.o objdump -Mintel -Mi386 -d test.o clean: rm test.o
test.o: file format elf32-i386 Disassembly of section .text: 00000000 <test>: 0: 55 push ebp 1: 89 e5 mov ebp,esp 3: 83 ec 10 sub esp,0x10 6: c7 45 f8 00 00 00 00 mov DWORD PTR [ebp-0x8],0x0 d: c6 45 f8 00 mov BYTE PTR [ebp-0x8],0x0 11: c6 45 f9 11 mov BYTE PTR [ebp-0x7],0x11 15: c6 45 fa 22 mov BYTE PTR [ebp-0x6],0x22 19: c6 45 fb 33 mov BYTE PTR [ebp-0x5],0x33 1d: 8d 45 f8 lea eax,[ebp-0x8] 20: 89 45 fc mov DWORD PTR [ebp-0x4],eax 23: 8b 45 fc mov eax,DWORD PTR [ebp-0x4] 26: c6 00 00 mov BYTE PTR [eax],0x0 29: 8b 45 fc mov eax,DWORD PTR [ebp-0x4] 2c: 83 c0 01 add eax,0x1 2f: c6 00 11 mov BYTE PTR [eax],0x11 32: 8b 45 fc mov eax,DWORD PTR [ebp-0x4] 35: 83 c0 02 add eax,0x2 38: c6 00 22 mov BYTE PTR [eax],0x22 3b: 8b 45 fc mov eax,DWORD PTR [ebp-0x4] 3e: 83 c0 03 add eax,0x3 41: c6 00 33 mov BYTE PTR [eax],0x33 44: 90 nop 45: c9 leave 46: c3 ret
以上展示了最简单的以数字下标的方式向“数组”和“指针指向的内容”存数据。
可以看出:
array
在内存中的位置是ebp - 0x08
至ebp - 0x05
四个字节,array
的存取,直接以ebp - 0x08
为基准。
pointer
在内存中的位置是DWORD PTR ebp - 0x04
,其中存储着array
的地址,通过pointer
存取array
时,需要进行“解地址”计算。
在c语言中,array
和pointer
用起来几乎一样。
但如果通过inline assembly
将array
和pointer
传入,那么array
就是[ebp - 0x08]
,直接代表了第0个元素,访问第N个元素需要:
lea eax, %[array] add eax, N mov [eax], 0xabcd
pointer
就是[ebp - 0x04]
,其中存储着array
的地址,访问第N个元素:
mov eax, %[pointer] add eax, N mov [eax], 0xabcd
这篇关于数组和指针的区别的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-26结对编程到底难不难?答案在这里
- 2024-06-19《2023版Java工程师》课程升级公告
- 2024-06-15matplotlib作图不显示3D图,怎么办?
- 2024-06-1503-Loki 日志监控
- 2024-06-1504-让LLM理解知识 -Prompt
- 2024-06-05做软件测试需要懂代码吗?
- 2024-06-0514-ShardingSphere的分布式主键实现
- 2024-06-03为什么以及如何要进行架构设计权衡?
- 2024-05-31全网首发第二弹!软考2024年5月《软件设计师》真题+解析+答案!(11-20题)
- 2024-05-31全网首发!软考2024年5月《软件设计师》真题+解析+答案!(21-30题)