关于指向指针的指针,这两个的运算过程 不理解
核心就是 (*(p+i))=m++;*(p+i)=m++; 究竟是如何运算的,看一下ida解析过程 也没看明白,只知道她们区别很大
#include <stdio.h>
#include <malloc.h>
int main()
{
int **p=(int**)malloc(sizeof(int)*3);
for(int i=0;i<3;i++)
{
p=(int*)malloc(sizeof(int)*3);
}
int m=1;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
*(p+i)=m++;
}
}
#以下是IDA解析结果
for ( j = 0; j <= 2; ++j )
{
for ( k = 0; k <= 2; ++k )
{
v0 = v5++;
*(_DWORD *)v2 = v0; //这里是*(p+i)=m++; 被解析成这样,所以错误
}
}
#endif
#以下是IDA解析结果
for ( j = 0; j <= 2; ++j )
{
for ( k = 0; k <= 2; ++k )
{
v0 = v5++;
*(_DWORD *)(v2+sizeof(int)*k]) = v0; //这里是(*(p+i))=m++; 被解析成这样,所以正确
}
}
#endif
return 0;
}
看反汇编代码就很清楚了
#include <stdio.h>
#include <malloc.h>
int main() {
int **p;
int n = 0;
n = 2;
p = malloc(sizeof(*p) * n);
for(size_t i = 0; i < n; ++i) {
p = malloc(sizeof(*p) * n);
}
int m = 1;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
(*(p + i)) = m++;
*(p + i) = m++;
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
printf("%4d", p);
}
printf("\n");
}
for(size_t i = 0; i < n; ++i) free(p);
free(p);
return 0;
}
int main() {
1169: 55 pushq%rbp
116a: 48 89 e5 movq %rsp,%rbp
116d: 53 pushq%rbx
116e: 48 83 ec 38 subq $0x38,%rsp
int **p;
int n = 0;
1172: c7 45 d4 00 00 00 00 movl $0x0,-0x2c(%rbp)
n = 2;
1179: c7 45 d4 02 00 00 00 movl $0x2,-0x2c(%rbp)
p = malloc(sizeof(*p) * n);
1180: 8b 45 d4 movl -0x2c(%rbp),%eax
1183: 48 98 cltq
1185: 48 c1 e0 03 shlq $0x3,%rax
1189: 48 89 c7 movq %rax,%rdi
118c: e8 cf fe ff ff callq1060 <malloc@plt>
1191: 48 89 45 e8 movq %rax,-0x18(%rbp)
for(size_t i = 0; i < n; ++i) {
1195: 48 c7 45 d8 00 00 00 movq $0x0,-0x28(%rbp)
119c: 00
119d: eb 2d jmp 11cc <main+0x63>
p = malloc(sizeof(*p) * n);
119f: 8b 45 d4 movl -0x2c(%rbp),%eax
11a2: 48 98 cltq
11a4: 48 c1 e0 02 shlq $0x2,%rax
11a8: 48 8b 55 d8 movq -0x28(%rbp),%rdx
11ac: 48 8d 0c d5 00 00 00 leaq 0x0(,%rdx,8),%rcx
11b3: 00
11b4: 48 8b 55 e8 movq -0x18(%rbp),%rdx
11b8: 48 8d 1c 11 leaq (%rcx,%rdx,1),%rbx
11bc: 48 89 c7 movq %rax,%rdi
11bf: e8 9c fe ff ff callq1060 <malloc@plt>
11c4: 48 89 03 movq %rax,(%rbx)
for(size_t i = 0; i < n; ++i) {
11c7: 48 83 45 d8 01 addq $0x1,-0x28(%rbp)
11cc: 8b 45 d4 movl -0x2c(%rbp),%eax
11cf: 48 98 cltq
11d1: 48 39 45 d8 cmpq %rax,-0x28(%rbp)
11d5: 72 c8 jb 119f <main+0x36>
}
int m = 1;
11d7: c7 45 c0 01 00 00 00 movl $0x1,-0x40(%rbp)
for(int i = 0; i < n; i++) {
11de: c7 45 c4 00 00 00 00 movl $0x0,-0x3c(%rbp)
11e5: eb 75 jmp 125c <main+0xf3>
for(int j = 0; j < n; j++) {
11e7: c7 45 c8 00 00 00 00 movl $0x0,-0x38(%rbp)
11ee: eb 60 jmp 1250 <main+0xe7>
(*(p + i)) = m++;
11f0: 8b 45 c0 movl -0x40(%rbp),%eax
11f3: 8d 50 01 leal 0x1(%rax),%edx
11f6: 89 55 c0 movl %edx,-0x40(%rbp)
11f9: 8b 55 c4 movl -0x3c(%rbp),%edx
11fc: 48 63 d2 movslq %edx,%rdx
11ff: 48 8d 0c d5 00 00 00 leaq 0x0(,%rdx,8),%rcx
1206: 00
1207: 48 8b 55 e8 movq -0x18(%rbp),%rdx
120b: 48 01 ca addq %rcx,%rdx
120e: 48 8b 0a movq (%rdx),%rcx
1211: 8b 55 c8 movl -0x38(%rbp),%edx
1214: 48 63 d2 movslq %edx,%rdx
1217: 48 c1 e2 02 shlq $0x2,%rdx
121b: 48 01 ca addq %rcx,%rdx
121e: 89 02 movl %eax,(%rdx)
*(p + i) = m++;
1220: 8b 45 c0 movl -0x40(%rbp),%eax
1223: 8d 50 01 leal 0x1(%rax),%edx
1226: 89 55 c0 movl %edx,-0x40(%rbp)
1229: 8b 55 c4 movl -0x3c(%rbp),%edx
122c: 48 63 ca movslq %edx,%rcx
122f: 8b 55 c8 movl -0x38(%rbp),%edx
1232: 48 63 d2 movslq %edx,%rdx
1235: 48 01 ca addq %rcx,%rdx
1238: 48 8d 0c d5 00 00 00 leaq 0x0(,%rdx,8),%rcx
123f: 00
1240: 48 8b 55 e8 movq -0x18(%rbp),%rdx
1244: 48 01 ca addq %rcx,%rdx
1247: 48 8b 12 movq (%rdx),%rdx
124a: 89 02 movl %eax,(%rdx)
for(int j = 0; j < n; j++) {
124c: 83 45 c8 01 addl $0x1,-0x38(%rbp)
1250: 8b 45 c8 movl -0x38(%rbp),%eax
1253: 3b 45 d4 cmpl -0x2c(%rbp),%eax
1256: 7c 98 jl 11f0 <main+0x87>
for(int i = 0; i < n; i++) {
1258: 83 45 c4 01 addl $0x1,-0x3c(%rbp)
125c: 8b 45 c4 movl -0x3c(%rbp),%eax
125f: 3b 45 d4 cmpl -0x2c(%rbp),%eax
1262: 7c 83 jl 11e7 <main+0x7e>
}
}
for(int i = 0; i < n; i++) {
1264: c7 45 cc 00 00 00 00 movl $0x0,-0x34(%rbp)
126b: eb 5e jmp 12cb <main+0x162>
for(int j = 0; j < n; j++) {
126d: c7 45 d0 00 00 00 00 movl $0x0,-0x30(%rbp)
1274: eb 3f jmp 12b5 <main+0x14c>
printf("%4d", p);
1276: 8b 45 cc movl -0x34(%rbp),%eax
1279: 48 98 cltq
127b: 48 8d 14 c5 00 00 00 leaq 0x0(,%rax,8),%rdx
1282: 00
1283: 48 8b 45 e8 movq -0x18(%rbp),%rax
1287: 48 01 d0 addq %rdx,%rax
128a: 48 8b 10 movq (%rax),%rdx
128d: 8b 45 d0 movl -0x30(%rbp),%eax
1290: 48 98 cltq
1292: 48 c1 e0 02 shlq $0x2,%rax
1296: 48 01 d0 addq %rdx,%rax
1299: 8b 00 movl (%rax),%eax
129b: 89 c6 movl %eax,%esi
129d: 48 8d 05 60 0d 00 00 leaq 0xd60(%rip),%rax # 2004 <_IO_stdin_used+0x4>
12a4: 48 89 c7 movq %rax,%rdi
12a7: b8 00 00 00 00 movl $0x0,%eax
12ac: e8 9f fd ff ff callq1050 <printf@plt>
for(int j = 0; j < n; j++) {
12b1: 83 45 d0 01 addl $0x1,-0x30(%rbp)
12b5: 8b 45 d0 movl -0x30(%rbp),%eax
12b8: 3b 45 d4 cmpl -0x2c(%rbp),%eax
12bb: 7c b9 jl 1276 <main+0x10d>
}
printf("\n");
12bd: bf 0a 00 00 00 movl $0xa,%edi
12c2: e8 79 fd ff ff callq1040 <putchar@plt>
for(int i = 0; i < n; i++) {
12c7: 83 45 cc 01 addl $0x1,-0x34(%rbp)
12cb: 8b 45 cc movl -0x34(%rbp),%eax
12ce: 3b 45 d4 cmpl -0x2c(%rbp),%eax
12d1: 7c 9a jl 126d <main+0x104>
}
for(size_t i = 0; i < n; ++i) free(p);
12d3: 48 c7 45 e0 00 00 00 movq $0x0,-0x20(%rbp)
12da: 00
12db: eb 23 jmp 1300 <main+0x197>
12dd: 48 8b 45 e0 movq -0x20(%rbp),%rax
12e1: 48 8d 14 c5 00 00 00 leaq 0x0(,%rax,8),%rdx
12e8: 00
12e9: 48 8b 45 e8 movq -0x18(%rbp),%rax
12ed: 48 01 d0 addq %rdx,%rax
12f0: 48 8b 00 movq (%rax),%rax
12f3: 48 89 c7 movq %rax,%rdi
12f6: e8 35 fd ff ff callq1030 <free@plt>
12fb: 48 83 45 e0 01 addq $0x1,-0x20(%rbp)
1300: 8b 45 d4 movl -0x2c(%rbp),%eax
1303: 48 98 cltq
1305: 48 39 45 e0 cmpq %rax,-0x20(%rbp)
1309: 72 d2 jb 12dd <main+0x174>
free(p);
130b: 48 8b 45 e8 movq -0x18(%rbp),%rax
130f: 48 89 c7 movq %rax,%rdi
1312: e8 19 fd ff ff callq1030 <free@plt>
return 0;
1317: b8 00 00 00 00 movl $0x0,%eax
}
131c: 48 8b 5d f8 movq -0x8(%rbp),%rbx
1320: c9 leaveq
1321: c3 retq
(*(p + i)) = m++;
11f0: 8b 45 c0 movl -0x40(%rbp),%eax
11f3: 8d 50 01 leal 0x1(%rax),%edx
11f6: 89 55 c0 movl %edx,-0x40(%rbp)
11f9: 8b 55 c4 movl -0x3c(%rbp),%edx
11fc: 48 63 d2 movslq %edx,%rdx
11ff: 48 8d 0c d5 00 00 00 leaq 0x0(,%rdx,8),%rcx
1206: 00
1207: 48 8b 55 e8 movq -0x18(%rbp),%rdx
120b: 48 01 ca addq %rcx,%rdx
120e: 48 8b 0a movq (%rdx),%rcx
1211: 8b 55 c8 movl -0x38(%rbp),%edx
1214: 48 63 d2 movslq %edx,%rdx
1217: 48 c1 e2 02 shlq $0x2,%rdx
121b: 48 01 ca addq %rcx,%rdx
121e: 89 02 movl %eax,(%rdx)
*(p + i) = m++;
1220: 8b 45 c0 movl -0x40(%rbp),%eax
1223: 8d 50 01 leal 0x1(%rax),%edx
1226: 89 55 c0 movl %edx,-0x40(%rbp)
1229: 8b 55 c4 movl -0x3c(%rbp),%edx
122c: 48 63 ca movslq %edx,%rcx
122f: 8b 55 c8 movl -0x38(%rbp),%edx
1232: 48 63 d2 movslq %edx,%rdx
1235: 48 01 ca addq %rcx,%rdx
1238: 48 8d 0c d5 00 00 00 leaq 0x0(,%rdx,8),%rcx
123f: 00
1240: 48 8b 55 e8 movq -0x18(%rbp),%rdx
1244: 48 01 ca addq %rcx,%rdx
1247: 48 8b 12 movq (%rdx),%rdx
124a: 89 02 movl %eax,(%rdx)
[]优先级高于*
所以
*(p + i)
*((p + i))
这两个是一样的
a 可以写成 *(a + b)
(p + i) 可以写成 *((p + i) + j),也就是 *(p + i + j)
*((p + i)) 就是 **(p + i + j)
*(p + i) = m++;
1220: 8b 45 c0 movl -0x40(%rbp),%eax
1223: 8d 50 01 leal 0x1(%rax),%edx
1226: 89 55 c0 movl %edx,-0x40(%rbp)
1229: 8b 55 c4 movl -0x3c(%rbp),%edx
122c: 48 63 ca movslq %edx,%rcx
122f: 8b 55 c8 movl -0x38(%rbp),%edx
1232: 48 63 d2 movslq %edx,%rdx
1235: 48 01 ca addq %rcx,%rdx
1238: 48 8d 0c d5 00 00 00 leaq 0x0(,%rdx,8),%rcx
123f: 00
1240: 48 8b 55 e8 movq -0x18(%rbp),%rdx
1244: 48 01 ca addq %rcx,%rdx
1247: 48 8b 12 movq (%rdx),%rdx
124a: 89 02 movl %eax,(%rdx) $ objdump -S -M suffix main 人造人 发表于 2022-5-20 08:48
收到
页:
[1]