whether this is valid

Discussion in 'C Programming' started by madhur, Jun 26, 2004.

  1. madhur

    madhur Guest

    Hello

    char *a="str";
    strcpy(a,"abc");

    I have learnt that since "a" is a string literal, it might be allocated on
    read only memory on some implementations.
    Is it correct to modify a string literal using this way.

    Madhur Ahuja
    India
    madhur, Jun 26, 2004
    #1
    1. Advertising

  2. madhur <> scribbled the following:
    > Hello


    > char *a="str";
    > strcpy(a,"abc");


    > I have learnt that since "a" is a string literal, it might be allocated on
    > read only memory on some implementations.
    > Is it correct to modify a string literal using this way.


    Not, it's not correct. It might work or "work" on some implementations
    but that's due to either special allowances or sheer dumb luck. The
    above code causes undefined behaviour and thus should never be relied
    on.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "As we all know, the hardware for the PC is great, but the software sucks."
    - Petro Tyschtschenko
    Joona I Palaste, Jun 26, 2004
    #2
    1. Advertising

  3. In article <>, "madhur" <>
    wrote:

    > Hello
    >
    > char *a="str";
    > strcpy(a,"abc");
    >
    > I have learnt that since "a" is a string literal, it might be allocated on
    > read only memory on some implementations.
    > Is it correct to modify a string literal using this way.


    If anyone tells you that it is, shoot him. If you are ever caught doing
    it on purpose in a professional job, you will be instantly fired.

    Now please tell us: If it were legal, why on earth would you do it?
    Christian Bau, Jun 26, 2004
    #3
  4. madhur

    Malcolm Guest

    "Christian Bau" <> wrote in message
    > > char *a="str";
    > > strcpy(a,"abc");
    > >

    > Now please tell us: If it were legal, why on earth would you do > it?
    >

    char *filename = "foo";

    /* lots of code using "filename" */

    Change to specs

    char *filename = "foo";

    if( filenotavailable() )
    strcpy(filename, "bar");

    /* lots of code unmodified from previous version */
    Malcolm, Jun 26, 2004
    #4
  5. In article <cbku61$3h2$>,
    Malcolm <> wrote:
    >if( filenotavailable() )
    > strcpy(filename, "bar");


    Why not

    filename = "bar";

    ?

    -- Richard
    Richard Tobin, Jun 27, 2004
    #5
  6. madhur

    CBFalconer Guest

    madhur wrote:
    >
    > char *a="str";
    > strcpy(a,"abc");
    >
    > I have learnt that since "a" is a string literal, it might be
    > allocated on read only memory on some implementations.
    > Is it correct to modify a string literal using this way.


    No. If you get in the habit of making such declarations as below,
    the compiler will usually warn you about misuse:

    const char *a = "str";

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Jun 27, 2004
    #6
  7. madhur

    tweak Guest

    CBFalconer wrote:
    > madhur wrote:
    >
    >>char *a="str";
    >>strcpy(a,"abc");
    >>
    >>I have learnt that since "a" is a string literal, it might be
    >>allocated on read only memory on some implementations.
    >>Is it correct to modify a string literal using this way.

    >
    >
    > No. If you get in the habit of making such declarations as below,
    > the compiler will usually warn you about misuse:
    >
    > const char *a = "str";
    >

    And that's because modifying the contents of a string pointed to
    results in undefined behavior:

    " ...the pointer may subsequently be modified to point elsewhere,
    but the result is undefined if you try to modify the string contents."

    K&R2 pg. 104

    This goes back to the differences between arrays and pointers.

    Brian

    P.S. I do not think this has changed since K&R2. Please correct me if
    I'm wrong.
    tweak, Jun 27, 2004
    #7
  8. "Malcolm" <> wrote in message
    news:cbku61$3h2$...
    > char *filename = "foo";
    >
    > if( filenotavailable() )
    > strcpy(filename, "bar");


    Why not make filename a char[]?

    S

    --
    Stephen Sprunk "Those people who think they know everything
    CCIE #3723 are a great annoyance to those of us who do."
    K5SSS --Isaac Asimov
    Stephen Sprunk, Jun 27, 2004
    #8
  9. madhur

    madhur Guest

    Hello
    Thanks, for all your answers.
    Now, If I do the following:

    char *a="str";
    a=malloc(100); /*str is lost*/
    strcpy(a,"abc");
    strcpy(a,"def");

    Is this OK. Memory allocated using memory should not be Read Only in this
    case.

    "madhur" <> wrote in message
    news:...
    > Hello
    >
    > char *a="str";
    > strcpy(a,"abc");
    >
    > I have learnt that since "a" is a string literal, it might be allocated on
    > read only memory on some implementations.
    > Is it correct to modify a string literal using this way.
    >
    > Madhur Ahuja
    > India
    >
    >
    >
    madhur, Jun 27, 2004
    #9
  10. madhur

    CBFalconer Guest

    madhur wrote:
    >
    > Thanks, for all your answers.
    > Now, If I do the following:
    >
    > char *a="str";
    > a=malloc(100); /*str is lost*/
    > strcpy(a,"abc");
    > strcpy(a,"def");
    >
    > Is this OK. Memory allocated using memory should not be Read Only
    > in this case.


    Please do not top-post. Your answer belongs after the material
    (suitably snipped) to which you are replying.

    That is perfectly legitimate. a can hold a pointer to char, and
    that is what you have given it via the malloc (but you failed to
    check for NULL). There is no memory leak, even though the "str"
    may become inaccessible, or may not, depending on the compiler.

    char *a = "str";
    char *b = "str";

    a may well end up the same as b. No guarantees.

    --
    Some useful references:
    <http://www.ungerhu.com/jxh/clc.welcome.txt>
    <http://www.eskimo.com/~scs/C-faq/top.html>
    <http://benpfaff.org/writings/clc/off-topic.html>
    <http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/> (C99)
    CBFalconer, Jun 27, 2004
    #10
  11. On Sun, 27 Jun 2004 02:19:43 +0530, "madhur" <> wrote:

    >Hello
    >
    >char *a="str";
    >strcpy(a,"abc");
    >
    >I have learnt that since "a" is a string literal, it might be allocated on
    >read only memory on some implementations.
    >Is it correct to modify a string literal using this way.
    >

    It is true that if there were a constant of the form "a" in your
    program, it would be a string literal. However, there is not.

    There is a pointer a which is initialized to point to a string
    literal. And no, it is not legal to modify a string literal in any
    way.

    Once you change the value in a so that it points to a modifiable area
    of memory, such as an array of at least 4 char or an allocated area of
    at least 4 bytes, your strcpy will work just fine.

    If a will always point to unmodifiable areas of memory, you might want
    to declare it const char *a so the compiler can detect if you try to
    change the data it points to.


    <<Remove the del for email>>
    Barry Schwarz, Jun 27, 2004
    #11
  12. In 'comp.lang.c', "madhur" <> wrote:

    > char *a="str";
    > strcpy(a,"abc");
    >
    > I have learnt that since "a" is a string literal, it might be allocated on
    > read only memory on some implementations.
    > Is it correct to modify a string literal using this way.


    It's not. This is why it is highly recommended that you write it

    char const *a = "str";

    strcpy (a, "abc");

    will now produce some warning with a decent and well tuned compiler or lint.
    Of course 'const' could be abused by some malicious typecast, but no code
    writer fitted with a working brain would do that... I guess...

    --
    -ed- get my email here: http://marreduspam.com/ad672570
    The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
    FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
    Emmanuel Delahaye, Jun 27, 2004
    #12
  13. In 'comp.lang.c', "madhur" <> wrote:

    > char *a="str";
    > a=malloc(100); /*str is lost*/


    What is the point in defining a string literal if you don't use it at all?

    What's wrong with :

    char *a = malloc(100);

    also, the value of a should be tested against NULL before a is used.

    if (a != NULL)
    {

    Also, don't forget to free the allocated bloc with free() once you don't need
    it any more.

    free (a), a = NULL;
    }

    > strcpy(a,"abc");
    > strcpy(a,"def");
    >
    > Is this OK. Memory allocated using memory should not be Read Only in this
    > case.


    Yes. The bloc of memory allocated by malloc() is guaranteed to belong to a
    read/write memory zone.

    --
    -ed- get my email here: http://marreduspam.com/ad672570
    The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
    FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
    Emmanuel Delahaye, Jun 27, 2004
    #13
  14. madhur

    Dan Pop Guest

    In <> "madhur" <> writes:

    >char *a="str";
    >strcpy(a,"abc");
    >
    >I have learnt that since "a" is a string literal, it might be allocated on
    >read only memory on some implementations.
    >Is it correct to modify a string literal using this way.


    Haven't you also learnt that you MUST check the FAQ before posting?

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Jun 28, 2004
    #14
  15. madhur <> wrote:
    > Hello


    > char *a="str";
    > strcpy(a,"abc");


    > I have learnt that since "a" is a string literal, it might be allocated on

    ^^^^^^^^^^^^^^^^^^^^^^^^
    > read only memory on some implementations.

    ^^^^^^^^^^^^^^^^
    > Is it correct to modify a string literal using this way.


    You answered your own question. You can't legally modify a string
    literal. However, you can make your pointer point to something else.

    a = "abc";

    or

    a = malloc(4);

    if(a != NULL)
    strcpy(a, "abc");

    Alternatively, you can declare 'a' to be an array to start with.

    char a[] = "str";

    strcpy(a, "abc");


    --
    Alex Monjushko ()
    Alex Monjushko, Jun 28, 2004
    #15
  16. "madhur" <> wrote in message news:<>...
    > Hello
    >
    > char *a="str";
    > strcpy(a,"abc");

    "str" actually doesn't exist in your (function) stack. BUT It gonna be
    located in data segment which is read only. (not 'a' itself, as you
    said)
    And your *a is a pointer (located in your stack that means it be able
    to modified). This variable pointed to address that "str" located.

    And then you tried 'strcpy(a,"abc");'. That means you tried to modify
    memory that a pointed to. Do you remember I just mentioned that it is
    read only data segment.

    This code is not gonna work. Let change to char a[]= "str"; like the
    other people in group said.

    In this case compiler will allocate 4 bytes(Ignor alignment here) for
    you in stack and copy "str" from data segment to that memory. And It
    can be modify because it's located in stack segment.

    Regards,
    Prawit Chaivong.
    >
    > I have learnt that since "a" is a string literal, it might be allocated on
    > read only memory on some implementations.
    > Is it correct to modify a string literal using this way.
    >
    > Madhur Ahuja
    > India
    Prawit Chaivong, Jun 29, 2004
    #16
  17. madhur

    Tuxpal Guest

    > In 'comp.lang.c', "madhur" <> wrote:
    >
    > char *a = malloc(100);
    > also, the value of a should be tested against NULL before a is used.
    > if (a != NULL)
    > {
    > Also, don't forget to free the allocated bloc with free() once you don't need
    > it any more.
    >
    > free (a), a = NULL;
    > }
    >
    > > strcpy(a,"abc");
    > > strcpy(a,"def");
    > >
    > > Is this OK. Memory allocated using memory should not be Read Only in this
    > > case.

    >
    > Yes. The bloc of memory allocated by malloc() is guaranteed to belong to a
    > read/write memory zone.


    a = NULL;

    Is this to avoid dangling pointers ?
    Is it needed always ?
    Tuxpal, Jun 29, 2004
    #17
  18. Tuxpal <> scribbled the following:
    >> In 'comp.lang.c', "madhur" <> wrote:
    >>
    >> char *a = malloc(100);
    >> also, the value of a should be tested against NULL before a is used.
    >> if (a != NULL)
    >> {
    >> Also, don't forget to free the allocated bloc with free() once you don't need
    >> it any more.
    >>
    >> free (a), a = NULL;
    >> }
    >>
    >> > strcpy(a,"abc");
    >> > strcpy(a,"def");
    >> >
    >> > Is this OK. Memory allocated using memory should not be Read Only in this
    >> > case.

    >>
    >> Yes. The bloc of memory allocated by malloc() is guaranteed to belong to a
    >> read/write memory zone.


    > a = NULL;


    > Is this to avoid dangling pointers ?


    > Is it needed always ?


    No, I would figure it's never needed. If you call free() on a block of
    memory, the block will still get freed, even if there are pointers
    pointing to it. You can't indirect through those pointers any more,
    though.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "I wish someone we knew would die so we could leave them flowers."
    - A 6-year-old girl, upon seeing flowers in a cemetery
    Joona I Palaste, Jun 29, 2004
    #18
  19. In 'comp.lang.c', (Tuxpal) wrote:

    >> free (a), a = NULL;


    > a = NULL;
    >
    > Is this to avoid dangling pointers ?


    Yes, once a bloc has been freed, the pointers holds an invalid address. I
    decide to give him a standard invalid value.

    > Is it needed always ?


    No. But I consider it good practice (D.P. may think that's is a religious
    issue, but I don't care). In my personnal rule of programming, a pointer
    should have 2 kind of possible values :

    NULL : meaning that the pointer is invalid.
    not NULL : meaning that the pointer points to a valid memory.

    --
    -ed- get my email here: http://marreduspam.com/ad672570
    The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
    FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
    Emmanuel Delahaye, Jun 29, 2004
    #19
  20. madhur

    Dan Pop Guest

    In <Xns95176BD3EFD7Bhsnoservernet@212.27.42.65> Emmanuel Delahaye <> writes:

    >In 'comp.lang.c', (Tuxpal) wrote:
    >
    >>> free (a), a = NULL;

    >
    >> a = NULL;
    >>
    >> Is this to avoid dangling pointers ?

    >
    >Yes, once a bloc has been freed, the pointers holds an invalid address. I
    >decide to give him a standard invalid value.

    ^^^^^^^^^^^^^
    If it were an invalid value, it would have been as good as any other
    invalid pointer value.

    A null pointer value is not an invalid value (except for a few, well
    defined operations: indirection and arithmetic). Most importantly,
    it can be used in pointer comparisons (for equality), while a genuine
    invalid pointer value cannot.

    As usual, there are cases where it makes sense to do what Emmanuel does
    and there are cases where it doesn't. But he is not expected to be able
    to understand this.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Jun 29, 2004
    #20
    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. Brad Smith
    Replies:
    2
    Views:
    478
    Blaise Pascal Tine
    Jun 27, 2003
  2. juppie
    Replies:
    5
    Views:
    30,991
    juppie
    Aug 30, 2006
  3. Joerg Hoehle
    Replies:
    0
    Views:
    373
    Joerg Hoehle
    Jan 24, 2007
  4. Shivanand Kadwadkar
    Replies:
    83
    Views:
    4,769
    Keith Thompson
    Jan 8, 2011
  5. Gene
    Replies:
    0
    Views:
    446
Loading...

Share This Page