Analysis of differences between C language (1) char * p and char p [] from the perspective of Assembly

Source: Internet
Author: User

 

C language code:

 

 

 

# Include <stdlib. h>

# Include <stdio. h>

 

Void main ()

{

Char array1 [] = {"123456 "};

Char * pointer1 = "123456 ";

}

Assembly code:

 

 

 

(Gdb) disassemble main

Dump of worker er code for function main:

0x08048394 <+ 0>: push % ebp

0x08048395 <+ 1>: mov % esp, % ebp

0x08048397 <+ 3>: sub $0x10, % esp

0x0804839a <+ 6>: movl $0x34333231,-0xb (% ebp)

0x080483a1 <+ 13>: movw $0x3635,-0x7 (% ebp)

0x080483a7 <+ 19>: movb $0x0,-0x5 (% ebp)

0x080483ab <+ 23>: movl $0x8048484,-0x4 (% ebp)

0x080483b2 <+ 30>: leave

0x080483b3 <+ 31>: ret

End of worker er dump.

(Gdb) x/7xb 0x8048484

0x8048484 <__dso_handle + 4>: 0x31 0x32 0x33 0x34 0x35 0x36 0x00

(Gdb)

From the <+ 6> <+ 13> <+ 19> lines, we can see that the program allocates 7 bytes of memory space for array1 to store "123456" + '\ 0 '.

The <+ 23> line indicates that the address 0x8048484 is assigned to pointer1. We can view the content after memory 0x8048484. The value of 7bytes is "123456" + '\ 0 ', here pointer1 is just a pointer and no memory units are allocated for it.

The following code is easy to understand.

 

 

 

# Include <stdlib. h>

# Include <stdio. h>

 

Void main ()

{

Char array1 [] = {"123456 "};

Char array2 [] = {"123456 "};

Char * pointer1 = "123456 ";

Char * pointer2 = "123456 ";

}

Assembly code:

 

 

(Gdb) disassemble main

Dump of worker er code for function main:

0x08048394 <+ 0>: push % ebp

0x08048395 <+ 1>: mov % esp, % ebp

0x08048397 <+ 3>: sub $0x20, % esp

0x0804839a <+ 6>: movl $0x34333231,-0xf (% ebp)

0x080483a1 <+ 13>: movw $0x3635,-0xb (% ebp)

0x080483a7 <+ 19>: movb $0x0,-0x9 (% ebp)

0x080483ab <+ 23>: movl $0x34333231,-0x16 (% ebp)

0x080483b2 <+ 30>: movw $0x3635,-0x12 (% ebp)

0x080483b8 <+ 36>: movb $0x0,-0x10 (% ebp)

0x080483bc <+ 40>: movl $0x8048494,-0x4 (% ebp)

0x080483c3 <+ 47>: movl $0x8048494,-0x8 (% ebp)

0x080483ca <+ 54>: leave

0x080483cb <+ 55>: ret

End of worker er dump.

(Gdb) x/7xb 0x8048494

0x8048494 <__dso_handle + 4>: 0x31 0x32 0x33 0x34 0x35 0x36 0x00

(Gdb)

 

Then the code will let it go!

 

(Gdb) disassemble main

Dump of worker er code for function main:

0x08048394 <+ 0>: push % ebp

0x08048395 <+ 1>: mov % esp, % ebp

0x08048397 <+ 3>: sub $0x20, % esp

0x0804839a <+ 6>: movl $0x34333231,-0xf (% ebp)

0x080483a1 <+ 13>: movw $0x3635,-0xb (% ebp)

0x080483a7 <+ 19>: movb $0x0,-0x9 (% ebp)

0x080483ab <+ 23>: movl $0x34333231,-0x16 (% ebp)

0x080483b2 <+ 30>: movw $0x3635,-0x12 (% ebp)

0x080483b8 <+ 36>: movb $0x0,-0x10 (% ebp)

0x080483bc <+ 40>: movl $0x8048494,-0x4 (% ebp)

0x080483c3 <+ 47>: movl $0x804849b,-0x8 (% ebp)

0x080483ca <+ 54>: leave

0x080483cb <+ 55>: ret

End of worker er dump.

(Gdb) x/7xb 0x8048494

0x8048494 <__dso_handle + 4>: 0x31 0x32 0x33 0x34 0x35 0x36 0x00

(Gdb) x/9xb 0x804849b

0x804849b <__dso_handle + 11>: 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38

0x80484a3 <__ dso_handle + 19>: 0x00

(Gdb)

 

From Study Notes of sg131971 (seven nights)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.