Segmentation fault around strcpy?
Asked Answered
G

4

5

I know that you will rap me over the knuckles but.

Why does it make Segmentation fault

char* cmd;
strcpy(cmd, argv[0]);

when this doesn't

char *cmd;
cmd = "plop";

I didn't practice since a while, and can't remember why.

ps: actually, i know that something like that, before the strcpy, would be better

char *cmd = (char*) malloc(strlen(argv[0]));

but i'm just wondering why this segmentation fault.

Thanks !

Gabbard answered 22/6, 2011 at 23:31 Comment(2)
possible duplicate of What's wrong with this C (strcpy) code?Taam
#9678154Taam
T
10

When you do:

char * cmd;

You're allocating a pointer on the stack. This pointer is not initialized to any meaningful value.

Then, when you do this:

strcpy(cmd, argv[0]);

You copy the string contained in argv[0] to the address pointed to cmd, which is... something meaningless. Since you're lucky, it simply segfaults.

When you do this:

cmd = "plop";

You assign to cmd the address to a statically allocated string constant. Since such strings are read only, writing on them is undefined behavior.

So, how to solve this? Allocate memory for the runtime to write to. There's two ways:

The first one is to allocate data on the stack, like this:

char cmd[100]; // for instance

This allocates an array of 100 chars on the stack. However, it's not necessarily robust, because you must know in advance how much memory you'll need. The stack is also smaller than the heap. Which leads us to option number 2:

char *cmd = malloc(whatever_you_need); // no need to cast, by the way, unless you're in C++

This allocates whatever_you_need chars on the heap. Don't forget to release the memory with free once you're done with it.

Tomasatomasina answered 22/6, 2011 at 23:42 Comment(0)
M
5

You get a seg. fault because cmd in your first example isn't pointing to anything (or, rather, it's pointing to something that's undefined - so attempting to read characters from or write characters to the pointer will probably result in an access violation).

In the second example, you're setting cmd to point to a legitimate string of chars.

Meiosis answered 22/6, 2011 at 23:34 Comment(0)
R
4

If you want to make copy of argv[0] easily,

char* cmd = strdup(argv[0]);

Of course, you have better to check result of strdup is null or not. :)

Ruff answered 23/6, 2011 at 0:34 Comment(0)
S
1

i'm just wondering why this segmentation fault.

Because if cmd is a global variable, its value is NULL, which is not writable, and if it's a local variable, then its value is indeterminate and you should not use it (but it can do anything if you do, which is worse than NULL in many cases).

Sylviesylvite answered 22/6, 2011 at 23:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.