[Help] Program crashes/seg-faults on strcpy().

Discussion in 'C Programming' started by a2x, Jul 1, 2005.

  1. a2x

    a2x Guest

    Hi, I've fixed this error, but I don't know why it occurs. Do you?

    Code:

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

    void another();

    int main()
    {
    char* temp;
    strcpy(temp, "Hi.\n");
    printf(temp);

    another();
    return(0);
    }

    void another()
    {
    char* temp1;
    strcpy(temp, "Hi again.\n");
    }


    Output:
    Hi.
    Segmentation fault


    The segmentation fault refers to the second call of strcpy(). If I
    don't call strcpy() the second time the program runs fine. Everything
    works fine if I replace 'char* temp1' with 'char temp1 = (int*) malloc
    (200)'.

    Do I have to use malloc before every call to strcpy()? Is my compiler
    (gcc) just randomly letting me off doing it the first time?

    Thanks.
    a2x, Jul 1, 2005
    #1
    1. Advertising

  2. In article <>,
    a2x <> wrote:

    > char* temp;
    > strcpy(temp, "Hi.\n");


    You haven't allocated any space for temp to point to.

    > char* temp1;
    > strcpy(temp, "Hi again.\n");


    You haven't allocated any space for temp1 to point to.

    >If I
    >don't call strcpy() the second time the program runs fine.


    Just luck. Both calls are wrong.

    >Do I have to use malloc before every call to strcpy()?


    That's almost right. You have to have space pointed to by the
    destination for each strcpy. You don't necessarily have to malloc it
    every time, if you happen to have it already pointing to enough space.

    -- Richard
    Richard Tobin, Jul 1, 2005
    #2
    1. Advertising

  3. "a2x" <> wrote in message
    news:...
    > Hi, I've fixed this error, but I don't know why it occurs. Do you?
    >
    > Code:
    >
    > #include <stdlib.h>
    > #include <string.h>
    >
    > void another();
    >
    > int main()
    > {
    > char* temp;
    > strcpy(temp, "Hi.\n");

    <snip>

    Whatever happens later in the code does not matter at this point.

    When you declare a pointer to char, the system gives you x bytes of data
    from the memory somewhere, which may contain the address of a zero
    terminated array of chars.

    The strcpy function assumes that it already does, and starts copying the
    contents of the second argument into it, which basically means writing bytes
    to a random place in the memory, which the computer may respond negatively
    to, such as a segfault.
    Martin Johansen, Jul 1, 2005
    #3
  4. a2x wrote:
    > Hi, I've fixed this error, but I don't know why it occurs. Do you?
    >
    > Code:
    >
    > #include <stdlib.h>
    > #include <string.h>
    >
    > void another();
    >
    > int main()
    > {
    > char* temp;
    > strcpy(temp, "Hi.\n");
    > printf(temp);
    >
    > another();
    > return(0);
    > }
    >
    > void another()
    > {
    > char* temp1;
    > strcpy(temp, "Hi again.\n");

    ^^^^
    1) There is no variable 'temp' in scope. This suggests that this is
    *not* the code you actually used.
    2) if you meant 'temp1', that is a wild pointer and you gave copied
    "Hi again.\n" into your grandfather's pacemaker.

    > }
    >
    >
    > Output:


    Not for the code you posted, which doesn't compile.

    > Hi.
    > Segmentation fault
    >
    Martin Ambuhl, Jul 1, 2005
    #4
  5. a2x

    Denis Kasak Guest

    "a2x" <> wrote in message
    news:...
    > Hi, I've fixed this error, but I don't know why it occurs. Do you?
    >
    > Code:
    >
    > #include <stdlib.h>
    > #include <string.h>


    #include <stdio.h> /* contains the declaration for the printf()
    function you used */

    > void another();
    >
    > int main()


    This should be int main(void).

    > {
    > char* temp;
    > strcpy(temp, "Hi.\n");


    You haven't allocated any space for 'temp' to point to, so this invokes
    undefined behaviour.

    > printf(temp);


    There is no declaration of printf() in scope because you failed to #include
    <stdio.h>.

    > another();
    > return(0);
    > }
    >
    > void another()
    > {
    > char* temp1;
    > strcpy(temp, "Hi again.\n");


    As someone already pointed out, there is no variable 'temp' in scope. When
    posting code, try copying and pasting it directly into your newsreader,
    rather than retyping it.
    Otherwise, if you have meant 'temp1' then, again, you do not allocate space
    for the pointer 'temp1' to point to and are copying your string literal in a
    random place in memory.

    > }
    >
    > Do I have to use malloc before every call to strcpy()? Is my compiler
    > (gcc) just randomly letting me off doing it the first time?


    You have to allocate space for the destination of strcpy(). One method of
    achieving this is calling malloc().

    -- Denis
    Denis Kasak, Jul 2, 2005
    #5
    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. Andrew King
    Replies:
    1
    Views:
    295
    John Harrison
    Apr 7, 2004
  2. Sharad Kala

    Re: malloc creates seg faults?

    Sharad Kala, Feb 14, 2005, in forum: C++
    Replies:
    2
    Views:
    297
    Sharad Kala
    Feb 14, 2005
  3. Berk Birand

    malloc creates seg faults?

    Berk Birand, Feb 14, 2005, in forum: C++
    Replies:
    7
    Views:
    368
    Artie Gold
    Feb 14, 2005
  4. Replies:
    4
    Views:
    401
    Ioannis Vranos
    Feb 15, 2005
  5. Jane Austine
    Replies:
    0
    Views:
    309
    Jane Austine
    Aug 14, 2003
Loading...

Share This Page