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

A

a2x

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.
 
R

Richard Tobin

a2x said:
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
 
M

Martin Johansen

a2x said:
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.
 
M

Martin Ambuhl

a2x said:
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.
 
D

Denis Kasak

a2x said:
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()

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
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top