[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: [OT] C++ question re. dyn. mem.



On Wed, 06 Aug 2003 06:00:11 +0200, MJM wrote:

> On Monday 04 August 2003 21:40, Sebastian Kapfer wrote:
>> > // change the way it is accessed to prove a point int * p_b = (int
>> >*)  p_a;
>>
>> Ouch.
> 
> Try this in /usr/src/linux/kernel
> 
> $ grep *\) *.c

This is unfair. First, it's kernel-land code, and second I am not aware of
any C++ code in The Kernel. Call me a fundamentalist, but I think such
dirty tricks have no gain in modern C++. (There is no alternative in C.)

>> > // p_a and p_b point to the same block of dyn. allocated memory;
>>
>> Do they?
> 
> They do. My app would be broken from the start if I could not rely on
> this capability.  This style of type conversion is covered in elementary
> C++ books by Bjarne.  It's not unusual.

Exactly where? I don't remember such casts from Bjarne's book. Maybe with
a big warning sign next to the code?

> You must be aware of what you are doing when you do a type conversion.
> Portability is a concern.  I am limiting my app to Intel 32 bit Linux.
> Screw everything else.

Well, if you must...

>> Watch out for inheritance, user-defined casting operators and other
>> funny stuff. C++ adds a few new meanings to the () casting syntax. In
>> general, your assumption is _wrong_.
> 
> I have no user defined casting operators or funny stuff.  I'm no fan of
> overloading.

What about inheritance? Not a fan either?

>> One should also note that the C-style casting operator is considered
>> bad style in C++. The "politically correct" way to rewrite your example
>> is
>>
>> int *p_b = reinterpret_cast<int *>(p_a);
> 
> By whom? Your example is nowhere to be found in my C++ books by Bjarne.
> So you are saying that Bjarne promotes bad style in his books? Why not
> tell him: http://www.research.att.com/~bs/homepage.html

My "Bjarne" somewhere recommends using the new-style cast operators. I
can't provide an actual quote though, since I have the German translation.
Maybe you have an older edition of the book; the third or fourth should
cover it.

BTW, there should be an article about the new casts in the C++ FAQ (I just
don't find it at the moment). I'm not going to replicate that stuff here,
since it is probably explained in more detail and better English than I
can provide. Maybe this can serve as a short intro:

http://www.hlrs.de/organization/tsc/services/tools/docu/kcc/UserGuide/chapter_9.html

Look in section 9.3, "New-Style Casts".

> Besides, reinterpret_cast is probably a template function doing this:
> 
> return ((T) x); // type conversion using cast

Erm... no. It is actually an operator built into the language. And it is
different from the C-style cast.

> That way, you're clearly stating the intent of the cast. It is up to
> your
>> compiler what it makes of this statement; the C++ standard doesn't
>> cover such abuse.
> 
> Language experts sure get their shorts knotted up over simple questions.

I'm not a language expert, and I'm not a killer programmer either :-)

> I've known some killer programmers and none of them have quoted a
> language specification in conversation.

We're talking about a corner case here.

> That was way over the top. That stuff is for compiler writers, not
> application programmers. I did not start a knowledge contest.  If I did
> inadvertently, then you win.

I don't look at it like a contest. There were two contradicting answers,
and I wanted to provide proof for mine. Sorry if the language spec was
over the top, but what else should I have quoted? I don't think I'll find
any C++ expert who debates our specific example of "delete".

> I just wanted to discuss a problem with others of similar interest and
> try to learn something.  I really don't care if my code style is bad,
> abusive, or politically incorrect.

Well, you now have my opinion on the issue. I won't go after you if you
prefer to think differently. Please don't feel offended.

-- 
Best Regards,   |   Hi! I'm a .signature virus. Copy me into
 Sebastian      |   your ~/.signature to help me spread!



Reply to: