(char *) to (const char *) is also dangerous but allowed?

Discussion in 'C Programming' started by lovecreatesbea...@gmail.com, Apr 9, 2007.

  1. Guest

    C stops the conversion from (char **) to (const char **). c-faq.com
    sec 11.10 has explanation on this point. But, for example, even the
    conversion from (char *) to (const char *) brings the same dangerous
    as in the previous conversion. Why the latter simple but dangerous one
    is allowed in C?

    $ cat f1.c
    int main(void)
    {
    const char c = 'a';
    char *p;
    const char *cp = p;

    cp = &c;
    *p = 'x'; /*line 8*/

    return 0;
    }
    $ cc -Aa -g f1.c
    $ ./a.out
    Bus error(coredump)
    $ gdb -q ./a.out core
    Core was generated by `a.out'.
    Program terminated with signal 10, Bus error.

    warning: The shared libraries were not privately mapped; setting a
    breakpoint in a shared library will not work until you rerun the
    program.

    #0 0x29d4 in main () at f1.c:8
    8 *p = 'x';
    (gdb) quit
    $
     
    , Apr 9, 2007
    #1
    1. Advertising

  2. On Apr 9, 10:53 am, ""
    <> wrote:
    > $ cat f1.c
    > int main(void)
    > {
    > const char c = 'a';
    > char *p;
    > const char *cp = p;
    >
    > cp = &c;
    > *p = 'x'; /*line 8*/
    >
    > return 0;}


    That crash has nothing to do with const char*. Remove all the lines
    involving cp and c, and the program will crash just the same. This is
    just a case of stupid programmer error.
     
    christian.bau, Apr 9, 2007
    #2
    1. Advertising

  3. <> wrote:
    > C stops the conversion from (char **) to (const char **). c-faq.com
    > sec 11.10 has explanation on this point. But, for example, even the
    > conversion from (char *) to (const char *) brings the same dangerous
    > as in the previous conversion. Why the latter simple but dangerous one
    > is allowed in C?


    > $ cat f1.c
    > int main(void)
    > {
    > const char c = 'a';
    > char *p;
    > const char *cp = p;


    > cp = &c;
    > *p = 'x'; /*line 8*/


    There isn't any issue with conversions between char and const char
    at all here. The simple and only problem is that you use a random
    address to store a value in. 'p' is never initialized, so there's
    no memory assigned to it that you own. Instead 'p' will point to
    some random position in memory. But you try to store a value at
    that memory location anyway. This invokes undefined behaviour and
    from now on anything can happen. You may get a segmentation fault,
    a bus error, it may even appear to work or the (in)famous nasal
    daemons could make their appearance.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Apr 9, 2007
    #3
  4. "" <> wrote in message
    >C stops the conversion from (char **) to (const char **). c-faq.com
    > sec 11.10 has explanation on this point. But, for example, even the
    > conversion from (char *) to (const char *) brings the same dangerous
    > as in the previous conversion. Why the latter simple but dangerous one
    >?

    It isn't dangerous to cast a pointer to a const *, as long as the data is
    set up correctly. Going in reverse from const * to a plain pointer is
    potentially dangerous, and is only allowed because of weaknesses in the
    language arising from the fact that const was an afterthought.
    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
     
    Malcolm McLean, Apr 9, 2007
    #4
  5. Guest

    On Apr 9, 4:03 am, (Jens Thoms Toerring) wrote:
    > <> wrote:
    > > C stops the conversion from (char **) to (const char **). c-faq.com
    > > sec 11.10 has explanation on this point. But, for example, even the
    > > conversion from (char *) to (const char *) brings the same dangerous
    > > as in the previous conversion. Why the latter simple but dangerous one
    > > is allowed in C?
    > > $ cat f1.c
    > > int main(void)
    > > {
    > > const char c = 'a';
    > > char *p;
    > > const char *cp = p;
    > > cp = &c;
    > > *p = 'x'; /*line 8*/

    >
    > There isn't any issue with conversions between char and const char
    > at all here. The simple and only problem is that you use a random
    > address to store a value in. 'p' is never initialized, so there's
    > no memory assigned to it that you own. Instead 'p' will point to
    > some random position in memory. But you try to store a value at
    > that memory location anyway. This invokes undefined behaviour and
    > from now on anything can happen. You may get a segmentation fault,
    > a bus error, it may even appear to work or the (in)famous nasal
    > daemons could make their appearance.


    I'm sorry to make a wrong example. The pointer p wasn't initiated. p
    and cp ever pointed to a same address, cp was re-assigned, p wasn't
    changed and had a random address all the time. I understand it now.
    Thank you for your time.
     
    , Apr 9, 2007
    #5
  6. wrote:
    > Why the latter simple but dangerous one
    > is allowed in C?


    Because the programmer is responsible for making sure that pointers have
    legal values. Your program self-destructs for a reason completely
    unrelated to the const-ness of the chars and and pointers-to-char.
    >
    > $ cat f1.c
    > int main(void)
    > {
    > const char c = 'a';
    > char *p;


    p is not initialized.

    > const char *cp = p;


    p is still not initialized, and the value of cp is indeterminate,


    > cp = &c;
    > *p = 'x'; /*line 8*/

    ^^
    dereferencing p, which has never been given a value.
    >
    > return 0;
    > }
     
    Martin Ambuhl, Apr 9, 2007
    #6
  7. Vallabha Guest

    On Apr 9, 5:53 am, ""
    <> wrote:
    > C stops the conversion from (char **) to (const char **). c-faq.com
    > sec 11.10 has explanation on this point. But, for example, even the
    > conversion from (char *) to (const char *) brings the same dangerous
    > as in the previous conversion. Why the latter simple but dangerous one
    > is allowed in C?
    >
    > $ cat f1.c
    > int main(void)
    > {
    > const char c = 'a';
    > char *p;
    > const char *cp = p;
    >
    > cp = &c;
    > *p = 'x'; /*line 8*/
    >
    > return 0;}
    >
    > $ cc -Aa -g f1.c
    > $ ./a.out
    > Bus error(coredump)
    > $ gdb -q ./a.out core
    > Core was generated by `a.out'.
    > Program terminated with signal 10, Bus error.
    >
    > warning: The shared libraries were not privately mapped; setting a
    > breakpoint in a shared library will not work until you rerun the
    > program.
    >
    > #0 0x29d4 in main () at f1.c:8
    > 8 *p = 'x';
    > (gdb) quit
    > $


    It's due to programming error.

    However, you can change the value of const char using a pointer.

    Ex:

    #include <stdio.h>


    int main ()
    {
    const char c = 'c';
    char *p = &c;
    *p = 'x';
    printf("%c \n", c);
    return 0;
    }

    However compiler thorws the warning of using a char pointer to a const
    character.

    $>cc del.c
    "del.c", line 7: warning: assignment type mismatch:
    pointer to char "=" pointer to const char

    Cheers
    -Vallabha
     
    Vallabha, Apr 11, 2007
    #7
  8. Flash Gordon Guest

    Vallabha wrote, On 11/04/07 06:30:

    <snip>

    > However, you can change the value of const char using a pointer.


    You can attempt to, but it invokes undefined behaviour which means that
    anything can happen, including it causing your in-laws to move in
    permanently. The most likely results are either the program crashing or
    working as you expect. Don't do it.

    > Ex:
    >
    > #include <stdio.h>
    >
    >
    > int main ()


    Better to be explicit
    int main(void)

    > {
    > const char c = 'c';
    > char *p = &c;
    > *p = 'x';
    > printf("%c \n", c);
    > return 0;
    > }
    >
    > However compiler thorws the warning of using a char pointer to a const
    > character.


    Some compilers do, but this is not required and not all compiler will.
    --
    Flash Gordon
     
    Flash Gordon, Apr 11, 2007
    #8
  9. Flash Gordon said:

    > [...] it invokes undefined behaviour which means
    > that anything can happen, including it causing your in-laws to move in
    > permanently.


    The UB war just got a little bit nastier.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
     
    Richard Heathfield, Apr 11, 2007
    #9
  10. >>>>> "RH" == Richard Heathfield <> writes:

    RH> Flash Gordon said:
    >> [...] it invokes undefined behaviour which means that anything
    >> can happen, including it causing your in-laws to move in
    >> permanently.


    RH> The UB war just got a little bit nastier.

    There's a reason the DS9000 was a commercial failure.

    Charlton


    --
    Charlton Wilbur
     
    Charlton Wilbur, Apr 11, 2007
    #10
  11. On Wed, 11 Apr 2007 07:48:49 +0100, Flash Gordon
    <> wrote:

    > Vallabha wrote, On 11/04/07 06:30:


    > > const char c = 'c';
    > > char *p = &c;


    > > However compiler thorws the warning of using a char pointer to a const
    > > character.

    >
    > Some compilers do, but this is not required and not all compiler will.


    Well, removing const or volatile qualification from a pointer is a
    constraint violation, and conforming implementation is required to
    issue a diagnostic. Whether that diagnostic is a warning, and how well
    it identifies the problem, is 'only' Quality of Implementation.

    - formerly david.thompson1 || achar(64) || worldnet.att.net
     
    David Thompson, Apr 19, 2007
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Santa
    Replies:
    1
    Views:
    1,122
    Mark A. Odell
    Jul 17, 2003
  2. Replies:
    24
    Views:
    854
    Netocrat
    Oct 30, 2005
  3. lovecreatesbeauty
    Replies:
    1
    Views:
    1,097
    Ian Collins
    May 9, 2006
  4. Javier
    Replies:
    2
    Views:
    585
    James Kanze
    Sep 4, 2007
  5. Iñaki Baz Castillo
    Replies:
    13
    Views:
    517
    Iñaki Baz Castillo
    May 1, 2011
Loading...

Share This Page