strcpy 周围的分段错误?
我知道你会敲打我的指关节,但是。
为什么它会导致分段错误,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您这样做时:
您正在堆栈上分配一个指针。该指针未初始化为任何有意义的值。
然后,当你这样做时:
你将 argv[0] 中包含的字符串复制到指向 cmd 的地址,这是......毫无意义的东西。既然你很幸运,它只是出现了段错误。
当您执行此操作时:
您将静态分配的字符串常量的地址分配给
cmd
。由于此类字符串是只读的,因此在其上写入是未定义的行为。那么,如何解决这个问题呢?为运行时写入分配内存。有两种方法:
第一种是在堆栈上分配数据,如下所示:
这会在堆栈上分配一个 100 个 char 的数组。但是,它不一定健壮,因为您必须提前知道需要多少内存。堆栈也比堆小。这就引出了选项 2:
这会在堆上分配
whatever_you_need
char
。使用完后,不要忘记使用free
释放内存。When you do:
You're allocating a pointer on the stack. This pointer is not initialized to any meaningful value.
Then, when you do this:
You copy the string contained in
argv[0]
to the address pointed tocmd
, which is... something meaningless. Since you're lucky, it simply segfaults.When you do this:
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:
This allocates an array of 100
char
s 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:This allocates
whatever_you_need
char
s on the heap. Don't forget to release the memory withfree
once you're done with it.你得到一个段。错误,因为第一个示例中的
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.
如果你想轻松地复制argv[0],
当然,你最好检查strdup的结果是否为空。 :)
If you want to make copy of argv[0] easily,
Of course, you have better to check result of strdup is null or not. :)
因为如果
cmd
是一个全局变量,它的值为NULL
,它是不可写的,如果它是一个局部变量,那么它的值是不确定的,你不应该使用它(但如果你这样做的话它可以做任何事情,这在很多情况下比 NULL 更糟糕)。Because if
cmd
is a global variable, its value isNULL
, 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).