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

The solution to the problem of C++ pointer Drift

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the solution to the problem of C++ pointer drift, the content is very detailed, interested friends can use for reference, I hope it can be helpful to you.

Although C++ programming language is powerful and flexible in application, there will also be a variety of errors in actual programming.

Recently we encountered a strange problem in our work. * determined that the C++ pointer drift caused by multi-inheritance is related to the C++ object model. The illustration is as follows:

Class A {...}; class B {...}; class AB: public B, public A {...}. AB * pab = new AB (); A * pa = (A*) pab; B* pb = (B*) pab

This is when you find that the values of pa and pb are not the same! One of them is equal to pab, while the other produces an offset. If you change the order of An and B in the declaration of AB, the pointer that produces the offset will become another.

To make sure that this is because the compiler made the conversion, fool the compiler with the void pointer:

Void* pv = (void*) pab; pa = (A*) pv

At this point, the value of pa is equal to that of pab, but it points to the wrong place. The conversion from pab to pa depends on the choice of path, which is not very reassuring. I don't know if there will be an error if I put the pointer in the container and then take it out. Of course, cast is used above, which should be avoided in good programs. If there is only implicit conversion, you can get the correct result:

Std::vector v; / / implicit type conversion v.insert (v.begin (), pab); void * pv = v [0]; pa = (A*) pv

The following programs are compiled using Cygwin/g++b:

# include

# include

Class A

{

Public:

Int a

}

Class B

{

Public:

Int b

}

Class AB: public B, public A

{

Public:

Int ab

}

Int main (int argc, char * * argv)

{

AB * pab = new AB ()

Pab- > ab = 1

Pab- > b = 2

Pab- > a = 3

A* pa = (A*) pab

B* pb = (B*) pab

Printf ("AB:% p\ n"\

"A:% p\ n"\

"B:% p\ n"

Pab, pa, pb)

Std::vector v

/ / implicit type conversion

V.insert (v.begin (), pab)

Void * pv = v [0]

Pa = (A*) pv

Printf ("pv is% p\ npa is% p\ npab% s pv\ n", pv, pa, (pab = = pv)

? "=": "

Printf ("A.an is% d\ n", pa- > a)

/ / forced type conversion

Pv = (void*) pab

Pa = (A*) pv

Printf ("Now A.an is% d\ n", pa- > a)

}

Running result:

AB: 0x6b01f0 A: 0x6b01f4 B: 0x6b01f0 pv is 0x6b01f4 pa is 0x6b01f4 pab! = pv A.an is 3 Now A.an is 2 solution to the C++ pointer drift problem is shared here. I hope the above content can be of some help to you and you can learn more knowledge. If you think the article is good, you can share it for more people to see.

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