strcpy 周围的分段错误?

发布于 2024-11-16 15:17:39 字数 352 浏览 0 评论 0原文

我知道你会敲打我的指关节,但是。

为什么它会导致分段错误,

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

而这不是

char *cmd;
cmd = "plop";

我已经有一段时间没有练习了,并且不记得为什么。

PS:实际上,我知道在 strcpy 之前类似的东西会更好,

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

但我只是想知道为什么会出现这种分段错误。

谢谢 !

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 !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

为人所爱 2024-11-23 15:17:39

当您这样做时:

char * cmd;

您正在堆栈上分配一个指针。该指针未初始化为任何有意义的值。

然后,当你这样做时:

strcpy(cmd, argv[0]);

你将 argv[0] 中包含的字符串复制到指向 cmd 的地址,这是......毫无意义的东西。既然你很幸运,它只是出现了段错误。

当您执行此操作时:

cmd = "plop";

您将静态分配的字符串常量的地址分配给 cmd。由于此类字符串是只读的,因此在其上写入是未定义的行为。

那么,如何解决这个问题呢?为运行时写入分配内存。有两种方法:

第一种是在堆栈上分配数据,如下所示:

char cmd[100]; // for instance

这会在堆栈上分配一个 100 个 char 的数组。但是,它不一定健壮,因为您必须提前知道需要多少内存。堆栈也比堆小。这就引出了选项 2:

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

这会在堆上分配 whatever_you_need char。使用完后,不要忘记使用 free 释放内存。

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.

·深蓝 2024-11-23 15:17:39

你得到一个段。错误,因为第一个示例中的 cmd 没有指向任何内容(或者更确切地说,它指向未定义的内容 - 因此尝试从指针读取字符或向指针写入字符可能< /em> 导致访问冲突)。

在第二个示例中,您将 cmd 设置为指向合法的字符串。

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.

眉目亦如画i 2024-11-23 15:17:39

如果你想轻松地复制argv[0],

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

当然,你最好检查strdup的结果是否为空。 :)

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. :)

〆一缕阳光ご 2024-11-23 15:17:39

我只是想知道为什么会出现这种分段错误。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文