🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #4679

... char * * promotion to char const * const * ...

#4679
From: Alexander Krotoff
Date: Fri, 19 Jan 1996 10:33
62 lines
2926 bytes
X-InCommentTo: john@polo.demon.co.uk (John Winters)

john@polo.demon.co.uk (John Winters) wrote:
> In article <KANZE.96Jan17121659@slsvewt.lts.sel.alcatel.de>,
> James Kanze US/ESC 60/3/141 #40763 <kanze@lts.sel.alcatel.de> wrote:
> [snip]
> >
> >Originally, the C standard was going to allow this; in fact, it was
> >going to allow all casts which added const anywhere in the type.  Then
> >someone pointed out that the conversion 'char ** -> char *const *' was
> >unsafe.  As a result, the wording was changed to only allow adding the
> >const at the top level.
> 
> Interesting.  Could you enlighten us (well, me anyway) as to *why* it
> is unsafe.  I don't find it immediately obvious.

Hello Joshn.

here is an example Joe Buck sent me this Julay.
i did not understand it too before.
i think it is quite simple and clean.

Regards,
--
Alexander N. Krotoff
Research Computer Center
Moscow State University

-------------------------------------------------------------------
From: Joe Buck <jbuck@Synopsys.COM>
Message-Id: <199507211636.JAA17469@deerslayer.synopsys.com>
Subject: Re: pointer conversions (minor)
To: krotoff@such.srcc.msu.su (Alexander Krotoff)
Date: Fri, 21 Jul 95 9:36:39 PDT
In-Reply-To: <9507211206.AA00906@such.srcc.msu.su>; from "Alexander Krotoff" at Jul 21, 95 3:10 pm
X-Mailer: ELM [version 2.3 PL11]

> As seems to me it's failed just becouse `ch_p' and `ch_pp' are never
> initialized ;-) Is your example complete ?

I apologize for the error.  Here is a correct example.  I saw the
segmentation fault and assumed it was due to accessing the const,
but the const object is on the stack so it's not in read-only memory.

#include <iostream.h>

int main() {
    const char ** const_ch_pp;
    char * ch_p;
    char ** ch_pp = & ch_p;
    const char const_ch = 'a';
    const char * const_ch_p = &const_ch;

    cout << "const_ch_p points to " << *const_ch_p << endl;
    const_ch_pp = ch_pp;        // illegal, but you want it legal
    *const_ch_pp = const_ch_p;  // copy const ptr to non-const ptr
    *ch_p = 'b';                // now we can change the const object
    cout << "const_ch_p points to " << *const_ch_p << endl;
}

It's simply not debatable: it is unsafe to assign a char** to a const char**.
--------------------------------------------------------------------
Technical Details
Message-ID: <4dns28$3c5@boy.nmd.msu.ru>
Path: arc1-ca.pugleaf.net!boy.nmd.msu.ru!not-for-mail
References: <4dgj8q$qin@unix.sri.com> <KANZE.96Jan17121659@slsvewt.lts.sel.alcatel.de> <DLBzGB.J60@polo.demon.co.uk>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!boy.nmd.msu.ru!not-for-mail
From: krotoff@such.srcc.msu.su (Alexander Krotoff)
Newsgroups: comp.std.c
Subject: ... char * * promotion to char const * const * ...
Date: 19 Jan 1996 13:33:44 +0300
Organization: Research Computer Center, Moscow State University
Sender: krotoff@boy.nmd.msu.ru
Message-ID: <4dns28$3c5@boy.nmd.msu.ru>
References: <4dgj8q$qin@unix.sri.com> <KANZE.96Jan17121659@slsvewt.lts.sel.alcatel.de> <DLBzGB.J60@polo.demon.co.uk>
Reply-To: krotoff@such.srcc.msu.su (Alexander Krotoff)
NNTP-Posting-Host: boy.nmd.msu.ru