Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis of examples in C++ where char [] can modify char* but not

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about the analysis of examples in C++ where char [] can modify char* but not. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Code

Int main (int argc, char * argv []) {char p [74] = "abcefghijkmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm"; char * a = "abcefghijkmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm"; printf ("% s, pjue a);}

Anyone can see this, and finally output abcefghijkmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm twice, no problem

Change the code again

Int main (int argc, char * argv []) {char p [74] = "abcefghijkmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm"; char * a = "abcefghijkmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm"; p [8] ='d é cor; a [8] ='d é cor; / / printf ("% s% s", pjue a);}

Run, report an error

The error points to a [8] ='d'

The error points to line 12, why Ni?

Look at the compilation.

You can see that the variables p and an are in the same way.

D used

Mov esi,offset string "abcefghijkmmmmmmmmmmmmmmmmmmmmmm"...

Assign the offset address of the string to the esi register

A used

Mov dword ptr [a], offset string "abcefghijkmmmmmmmmmmmmmmmmmmmmmm"...

Assign the offset address of the string to the address where the a variable is located

If you take a closer look at the two sentences in the picture, the addressing operation for the string comes from the same place, 01007500h, that is, the location of the program data segment in memory.

Since both operations are on the same string, why can some of them be modified or not?

There is a problem with the storage of programs after compiling files, such as the example above.

Variable aline p, they all operate on the same string, and the two strings are exactly the same, so after the program is compiled, there is no need to save two identical contents in the generated file, only one can be kept, so your program, no matter how many times you use this string, is actually referenced from one place, that is, these two lines of code.

Mov esi,offset string "abcefghijkmmmmmmmmmmmmmmmmmmmmmm"... mov dword ptr [a], offset string "abcefghijkmmmmmmmmmmmmmmmmmmmmmm"...

What it means.

But there is a problem, if two or more variables use the same string, and then what you want in the end, you have to modify it, and as a result, all the variables that reference this string have changed, so

A [8] ='d'

If you want to modify the data segment directly, you will report an error.

But p can, why, because the array should be handled differently, look at the code

Mov esi,offset string "abcefghijkmmmmmmmmmmmmmmmmmmmmmm"... (0977588h) lea edi, [p] rep movs dword ptr es: [edi], dword ptr [esi]

First, give the address of the string to esi, and then give the p address to edi

Then, execute through the rep movs loop, assign the character at [esi] to [edi], that is, copy a copy of the string to p

So, what you operate

P [8] ='d'

In fact, you manipulate a new string, not the string in the data segment

Let's take a look at the operation of a

Mov dword ptr [a], offset string "abcefghijkmmmmmmmmmmmmmmmmmmmmmm"... mov eax,1 shl eax,3 mov ecx,dword ptr [a] mov byte ptr [ecx+eax], 64h

First, you give 1 to eax, then shift 3, change EAX to 8, give the address of a, that is, the address of the string, to ecx, and then assign 64h, that is, d, to the position of [ecx+eax], that is, the position of j, so you are operating on the string in the data segment, which is the reason above. The system will prevent you from modifying the data segment, thus reporting an error.

Thank you for reading! On "C++ char [] can modify char* but not the example analysis" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report