简体   繁体   English

strcpy周围的分段错误?

[英]Segmentation fault around strcpy?

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 ps:实际上,我知道在strcpy之前会有更好的东西

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

but i'm just wondering why this segmentation fault. 但我只是想知道为什么这个分段错误。

Thanks ! 谢谢 !

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. 您将argv[0]包含的字符串复制到指向cmd的地址,这是......毫无意义的。 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. 您将cmd的地址分配给静态分配的字符串常量。 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 char s on the stack. 这将在堆栈上分配一个包含100个char的数组。 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: 这导致我们选择2号选项:

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

This allocates whatever_you_need char s on the heap. 这会在堆上分配whatever_you_need char Don't forget to release the memory with free once you're done with it. 一旦完成,不要忘记free释放内存。

You get a seg. 你得到一个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). 因为你的第一个例子中的cmd没有指向任何东西(或者更确切地说,它指向未定义的东西 - 因此尝试从指针读取字符或将字符写入指针可能会导致访问冲突)。

In the second example, you're setting cmd to point to a legitimate string of chars. 在第二个示例中,您将cmd设置为指向合法的字符串。

If you want to make copy of argv[0] easily, 如果你想轻松复制argv [0],

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

Of course, you have better to check result of strdup is null or not. 当然,你最好检查strdup的结果是否为null。 :) :)

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). 因为如果cmd是一个全局变量,它的值是NULL ,这是不可写的,如果它是一个局部变量,那么它的值是不确定的,你不应该使用它(但它可以做任何事情,如果你这样做,这比在许多情况下为NULL)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM