strlen runtime error after call strcpy

Discussion in 'C Programming' started by Duke, Apr 18, 2007.

  1. Duke

    Duke Guest

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    int main(int argc, char *argv[])
    {
    char *s = "hello strlen";
    printf("%s has %d chars.\n", s, strlen(s));
    //the above strlen function execute correctly
    char *msg1 = "abcdefghijklmnopqrstuvwxyz";

    char buf[10];

    strcpy(buf, msg1);
    printf("[%s] length: %d\n", msg1, strlen(msg1));
    //but the above statement will throw a runtime os exception
    system("PAUSE");
    return 0;
    }

    I don't know what occus after I find all the resource about c which I
    can find.
     
    Duke, Apr 18, 2007
    #1
    1. Advertising

  2. Duke

    Duke Guest

    PS: I found when I change the definition of buf from 'char buf[10]' to
    'char *buf', then it execute correctly.

    I don't know why? What dissimilitude char array and the char pointer
     
    Duke, Apr 18, 2007
    #2
    1. Advertising

  3. Duke said:

    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <string.h>
    >
    > int main(int argc, char *argv[])
    > {
    > char *s = "hello strlen";
    > printf("%s has %d chars.\n", s, strlen(s));
    > //the above strlen function execute correctly
    > char *msg1 = "abcdefghijklmnopqrstuvwxyz";
    >
    > char buf[10];
    >
    > strcpy(buf, msg1);


    buf is an array of 10 characters. Since a string is a sequence of
    characters terminated by the first null character, it follows that buf
    has sufficient storage to store a string of at most nine non-null
    characters. Assuming that's the whole alphabet you have msg1 pointing
    to (I didn't check carefully), you will require 27 bytes of storage in
    buf - the 10 is just insufficient.

    Once you trash your buffer in this way, the subsequent behaviour of the
    program is undefined.

    --
    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 18, 2007
    #3
  4. Duke said:

    > PS: I found when I change the definition of buf from 'char buf[10]' to
    > 'char *buf', then it execute correctly.


    No, it doesn't. It just fails to break in quite the same way. In this
    case, it's broken in a way that you don't happen to notice at the
    moment.

    > I don't know why? What dissimilitude char array and the char pointer


    An array is a place in which to keep things. A pointer is a signpost,
    for showing how to get to things. You can point a signpost at a city,
    but you can't store a city in a signpost.

    --
    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 18, 2007
    #4
  5. Richard Heathfield wrote:
    > Duke said:
    >
    >> PS: I found when I change the definition of buf from 'char buf[10]' to
    >> 'char *buf', then it execute correctly.

    >
    > No, it doesn't. It just fails to break in quite the same way. In this
    > case, it's broken in a way that you don't happen to notice at the
    > moment.
    >
    >> I don't know why? What dissimilitude char array and the char pointer

    >
    > An array is a place in which to keep things. A pointer is a signpost,
    > for showing how to get to things. You can point a signpost at a city,
    > but you can't store a city in a signpost.
    >

    Nice analogy.
     
    Clever Monkey, Apr 18, 2007
    #5
  6. Duke

    Default User Guest

    Duke wrote:

    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <string.h>
    >
    > int main(int argc, char *argv[])
    > {
    > char *s = "hello strlen";
    > printf("%s has %d chars.\n", s, strlen(s));
    > //the above strlen function execute correctly
    > char *msg1 = "abcdefghijklmnopqrstuvwxyz";
    >
    > char buf[10];
    >
    > strcpy(buf, msg1);
    > printf("[%s] length: %d\n", msg1, strlen(msg1));
    > //but the above statement will throw a runtime os exception
    > system("PAUSE");
    > return 0;
    > }
    >
    > I don't know what occus after I find all the resource about c which I
    > can find.



    What exactly are you trying to accomplish here? Either you don't know
    how to work strings, or you're deliberately trying broken code to see
    what happens.

    If the former, read over your text or FAQ sections dealing with
    strings. If the latter, stop. It tells you very little, and wastes
    everybody's time. There is no defined behavior for Undefined Behavior.




    Brian
     
    Default User, Apr 18, 2007
    #6
  7. On 18 Apr 2007 09:59:27 -0700, in comp.lang.c , Duke
    <> wrote:

    >#include <stdio.h>
    >#include <stdlib.h>
    >#include <string.h>
    >
    >int main(int argc, char *argv[])
    >{
    > char *s = "hello strlen";
    > printf("%s has %d chars.\n", s, strlen(s));
    > //the above strlen function execute correctly
    > char *msg1 = "abcdefghijklmnopqrstuvwxyz";
    >
    > char buf[10];
    >
    > strcpy(buf, msg1);


    Error - you just copied 25 or so characters into a space that can only
    hold ten. The memory used by your programme is now corrupted, and
    anything could happen....

    > printf("[%s] length: %d\n", msg1, strlen(msg1));
    > //but the above statement will throw a runtime os exception


    ..... including a runtime exception

    Fix: don't try to overfill things.

    For comparison, what happens if you try to put a five gallons of beer
    into a human? It overflows, probably exceptionally...
    --
    Mark McIntyre

    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
     
    Mark McIntyre, Apr 18, 2007
    #7
  8. Duke

    CBFalconer Guest

    Duke wrote:
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <string.h>
    >
    > int main(int argc, char *argv[])
    > {
    > char *s = "hello strlen";
    > printf("%s has %d chars.\n", s, strlen(s));
    > //the above strlen function execute correctly
    > char *msg1 = "abcdefghijklmnopqrstuvwxyz";
    >
    > char buf[10];
    >
    > strcpy(buf, msg1);


    Your program has involved undefined behaviour here. buf is not
    large enough. In addition, unless you have a C99 compiler, the
    declaration of buf is invalid. Move it up after the declaration of
    s.

    > printf("[%s] length: %d\n", msg1, strlen(msg1));
    > //but the above statement will throw a runtime os exception
    > system("PAUSE");


    This may or may not do anything.

    > return 0;
    > }
    >
    > I don't know what occus after I find all the resource about c which I
    > can find.


    Also, without a C99 compiler, the // comments may be illegal.

    --
    <http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
    <http://www.securityfocus.com/columnists/423>
    <http://www.aaxnet.com/editor/edit043.html>

    "A man who is right every time is not likely to do very much."
    -- Francis Crick, co-discover of DNA
    "There is nothing more amazing than stupidity in action."
    -- Thomas Matthews



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Apr 19, 2007
    #8
    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. Imran

    strlen

    Imran, Aug 12, 2004, in forum: C++
    Replies:
    2
    Views:
    541
    Jerry Coffin
    Aug 12, 2004
  2. lynology

    Bad File Descriptor Error on strcat/strcpy

    lynology, Aug 18, 2004, in forum: C Programming
    Replies:
    4
    Views:
    508
    Chris Torek
    Aug 18, 2004
  3. Matt
    Replies:
    82
    Views:
    1,797
    Douglas A. Gwyn
    Sep 29, 2004
  4. kaizen
    Replies:
    3
    Views:
    4,709
    Jim Langston
    Jan 21, 2006
  5. coinjo
    Replies:
    4
    Views:
    520
    red floyd
    Mar 1, 2006
Loading...

Share This Page