内存分配如何与字符指针(字符串文字,数组)一起使用?
目前正在阅读K& r,刚刚偶然发现了炭指针。在RN中定义字符指针时,记忆分配没有什么,也许以后会解释。但这只是没有意义,所以我正在寻求帮助:)
1
// No errors
char *name;
char *altname;
strcpy(altname,name);
2
// No errors, internals of *name have been successfully moved to *altname
char *name = "HI";
char *altname;
strcpy(altname, name);
3
// Segmentation fault, regardless of how I define *altname
char *name = "HI";
char *altname = "randomstring";
strcpy(altname, name);
4
// Segmentation fault, regardless of how I define *altname
char *name;
char *altname = " ";
strcpy(altname, name);
5
// copies internals of *name only if size of char s[] > 8???
char s[9];
char n[] = {'c', 'b'};
char *name = n;
char *altname = s;
strcpy(altname, name);
为什么第一个示例也不会产生错误,即使没有分配的内存?
为什么第二个成功将名称复制到AltName,即使没有为AltName分配的内存;
为什么第三和第三核心转储?
为什么第五个需要S as as as> 8?
Currently reading K&R and just got stumbled across the char pointers. There's nothing about memory allocation when defining char pointers in the book rn, maybe it'll be explained later. But it just doesn't make sense so I'm seeking help :)
1
// No errors
char *name;
char *altname;
strcpy(altname,name);
2
// No errors, internals of *name have been successfully moved to *altname
char *name = "HI";
char *altname;
strcpy(altname, name);
3
// Segmentation fault, regardless of how I define *altname
char *name = "HI";
char *altname = "randomstring";
strcpy(altname, name);
4
// Segmentation fault, regardless of how I define *altname
char *name;
char *altname = " ";
strcpy(altname, name);
5
// copies internals of *name only if size of char s[] > 8???
char s[9];
char n[] = {'c', 'b'};
char *name = n;
char *altname = s;
strcpy(altname, name);
Why does the first example produce no error, even though there's no memory allocated?
Why does the second one successfully copy name to altname, even though there's no memory allocated for altname;
Why do the third and forth one core dump?
Why does the fifth one require size of s as >8?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我有点惊讶前两个示例起作用。
strcpy
将第二个参数指向的字符阵列将其复制到第一个参数指向的字符数组。在您的第一个示例中,指针
名称
和AltName
没有指向任何内容。它们是非直接的指针。现在,当执行main
函数或只是编译器决定初始化它们时,它们具有基于内存中的任何内容的值。这就是为什么我惊讶于您的代码没有崩溃的原因。无论如何,您的第三个示例是segfaulting的原因是,您将两个指针都设置为字符串文字(即,
“ hi”
和“ andandstring”
)。字符串文字(很可能)存储在仅读取的内存部分中。因此,当您运行strcpy
将目标指针设置为altname
时,您正在尝试写入仅阅读内存。这是对内存的无效使用,因此崩溃了。您的第四个例子也是如此。在最后一个示例中,您的问题是
n
未终止。strcpy
继续复制字符,直到达到终结器(即,'\ 0''
)。您已将'C'
和a'b'
放在数组中,但没有终结器。因此,strcpy
即使达到n
的末尾,也将继续复制。当sizeof(s)
大于8时,它起作用的事实可能涉及堆栈中存在的空字节。也就是说,如果您使s
太小,您将在其末端编写并损坏堆栈上的内存。您可能会覆盖返回指针,从而在执行功能后返回到一些无效的内存地址。您需要做的是使用指针,而是使用数组。这将为您提供可写入的记忆部分所需的存储空间。您还需要确保阵列为无效。可以通过使用字符串文字初始化阵列来自动完成。
I'm a bit surprised that the first two examples worked.
strcpy
takes the character array pointed to by the second argument and copies it to the character array pointed to by the first argument.In your first example, the pointers
name
andaltname
aren't pointing to anything. They're uninitialized pointers. Now they have some value based on either whatever gunk was in memory when themain
function was executed or just however the compiler decided to initialize them. That's why I'm surprised that your code isn't crashing.Anyway, the reason why your third example is segfaulting is you've set both pointers to point to string literals (i.e.,
"HI"
and"randomstring"
). String literals are (most likely) stored in a read-only section of memory. So, when you runstrcpy
with the destination pointer set toaltname
, you're trying to write to read-only memory. That's an invalid use of memory and hence the crash. The same goes for your fourth example.In the final example, your problem is that
n
is not null-terminated.strcpy
keeps copying characters until it reaches a terminator (i.e.,'\0'
). You've put a'c'
and a'b'
in your array but no terminator. Therefore,strcpy
is going to keep copying even after it's reached the end ofn
. The fact that it works whensizeof(s)
is greater than 8 probably involves where a null byte happens to exist on your stack. That is, if you makes
too small, you'll write past the end of it and corrupt the memory on your stack. It's possible you're overriding your return pointer and thus returning to some invalid memory address when your function is done executing.What you need to do is, instead of using pointers, use arrays. This will give you the storage space you need in a writable section of memory. You also need to make sure that your arrays are null-terminated. This can be done automatically by initializing your arrays with string literals.
所有代码段都是未定义的行为。不能保证您会遇到错误。正如该术语所说,行为是不确定的。
在1和2中,一个或两个指示器是非传统化的,可能指出的内存可能会受到或可能不会受到编程的访问的保护,因此您可能会或可能不会遇到错误或程序崩溃。
在3和4中,您可能会得到可重现的分割故障,因为
altname
指向字符串字面“随机串”
或“”
可以读取 - 仅,因此不允许您覆盖altname
指向的内存。在4中,name
是非初始化的,因此这也可能是分割故障的原因。在5中,
n
不是可用于strcpy
的有效字符串。终止'\ 0'
缺少。strcpy
将(尝试)在n
结束后复制所有内容,直到找到'\ 0'
。修复使用或
All code snippets are undefined behavior. There is no guarantee that you will get an error. As the term says, the behavior is undefined.
In 1 and 2, one or both pointers are uninitialized and may point to memory that may or may not be protected against the programmed access, so you may or may not get an error or program crash.
In 3 and 4 you may get a reproducible segmentation fault because
altname
points to a string literal"randomstring"
or" "
which can be read-only, so you are not allowed to overwrite the memory wherealtname
points to. In 4,name
is uninitialized, so this might also be the cause of the segmentation fault.In 5,
n
is not a valid string that could be used forstrcpy
. The terminating'\0'
is missing.strcpy
will (try to) copy everything after the end ofn
until it finds a'\0'
. To fix it useor