为什么在 c 中使用 strcpy 时我的源代码发生了变化

发布于 2024-10-23 13:46:39 字数 664 浏览 5 评论 0原文

使用 strcpy 源后,源已损坏并获得了正确的目标。以下是我的代码,请告诉我为什么我的源代码被损坏?如果我将第二个字符数组 q[] 保持固定大小,那么我的源不会被更改。为什么这是奇怪的行为。 -
我正在使用 MSVC 2005

void function(char* str1,char* str2);
void main()
{

    char p[]="Hello world";
    char q[]="";
    function(p,q);
    cout<<"after function calling..."<<endl;
    cout<<"string1:"<<"\t"<<p<<endl;
    cout<<"string2:"<<"\t"<<q<<endl;
    cin.get();
}

void function(char* str1, char* str2)
{
    strcpy(str2,str1);
}

输出:

after function calling...
string1:        ld
string2:        Hello world

提前致谢,
马拉地语

After using strcpy source is getting corrupted and getting correct destination. Following is my code please suggest me why my source is getting corrupted? If i keep a fixed size to second character array q[] then my source is not being changed. Why is this strange behaviour. -
I am using MSVC 2005

void function(char* str1,char* str2);
void main()
{

    char p[]="Hello world";
    char q[]="";
    function(p,q);
    cout<<"after function calling..."<<endl;
    cout<<"string1:"<<"\t"<<p<<endl;
    cout<<"string2:"<<"\t"<<q<<endl;
    cin.get();
}

void function(char* str1, char* str2)
{
    strcpy(str2,str1);
}

OUTPUT:

after function calling...
string1:        ld
string2:        Hello world

Thanks in advance,
Malathi

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

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

发布评论

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

评论(7

旧故 2024-10-30 13:46:39

strcpy 不分配存储字符串所需的内存。
在执行 strcpy 之前,您必须在 str2 中分配足够的内存。
否则,当您覆盖一些未分配的内存时,您会得到未定义的行为。

strcpy does not allocate memory required to store the string.
You must allocate enough memory in str2 before you do the strcpy.
Otherwise, you get undefined behaviour as you are overwriting some non-allocated memory.

蓝梦月影 2024-10-30 13:46:39

q 仅具有 1 个字符的空间,即终止 \0
请读一本关于 C 的书——你需要学习一些关于内存管理的知识。

您的记忆很可能如下所示(简化):Qpppppppppppp。因此,当您 strcpy 到 q 时,您将覆盖 p 的部分内存。

由于您使用的是 C++:只需使用 std::string 和/或 std::stringstream 而不是原始 char 数组。

q has only space for 1 character which is the terminating \0.
Please read a book about C - you need to learn something about memory management.

Most likely your memory looks like this (simplified): Qpppppppppppp. So when you strcpy to q, you will overwrite parts of p's memory.

Since you are using C++: Simply use std::string and or std::stringstream instead of raw char arrays.

平生欢 2024-10-30 13:46:39

在您的代码中,q 是一个单元素数组(基于 "" 的长度,由于空终止符而等于 1),因此它不能包含整个字符串。因此,您无法执行 strcpy,因为它会覆盖无效的内存位置(尝试向数组写入太多数据)。

声明 q 足够大以包含您的字符串。另外,为了安全起见,您可以使用 strncpy

In your code, q, is an one-element array (basing on the length of "", which is equal to one due to the null terminator), so it cannot contain the whole string. Hence you can't do a strcpy because it writes over invalid memory location (tries to write too much data to an array).

Declare q to be big enough to contain your string. Also, you can use strncpy to be on the safe side.

初相遇 2024-10-30 13:46:39

char q[] = ""; 创建一个只有 1 个元素的字符数组 - 将更多数据复制到其中不会为其保留更多内存。

因此,当您写入为 q 保留的空间时,就会开始覆盖 p 中的内容 - 这两个变量在内存中彼此相邻。

char q[] = ""; creates a character array with exactly 1 element - copying more data into it won't reserve more memory for it.

So, what happens is that when you write past the space reserved for q, you start overwriting what's in p - the two variables are next to each other in memory.

油饼 2024-10-30 13:46:39

大家说的都对了一半。该代码失败是因为没有像其他人正确指出的那样为副本保留空间。缺少的部分是您的对象位于堆栈上,而不是堆上。因此,由于堆栈无法再展开,您的代码不仅可能而且不可避免地会被损坏。

What everyone is saying is half correct. The code is failing because space is not reserved for the copy as others have pointed out correctly. The part that's missing is that your objects are on the stack, not the heap. Therefore it is not only likely, but inevitable that your code will get corrupted as the stack can no longer be unwound.

东走西顾 2024-10-30 13:46:39

数组“q”只有一个字节长;它绝对没有空间容纳字符串“Hello, World”!当您尝试将“Hello, World”复制到 q 时,最终会超出 q 的边界并覆盖堆栈上与其相邻的 p。我想象画一个图表来说明这些东西如何在堆栈上布局,您可以准确地确定为什么 p 中最终的垃圾只是“ld”。

The array "q" is just one byte long; it definitely doesn't have room for the string "Hello, World"! When you try to copy "Hello, World" to q, you end up exceeding the bounds of q and overwriting p, which is adjacent to it on the stack. I imagine drawing a diagram of how these things are laid out on the stack, you could determine exactly why the garbage that ends up in p is just "ld".

烟燃烟灭 2024-10-30 13:46:39

strcpy 希望您提供一个分配的存储缓冲区,而不仅仅是一个 char* 指针。如果将 char q[]=""; 更改为 char q[50]; 它将起作用。由于您只为 strcpy 提供一个指向零长度字符串的指针,因此它没有足够的空间来存储复制的字符串,并且覆盖又会损坏内存。

strcpy expects you to provide an allocated storage buffer, not just a char* pointer. If you change char q[]=""; to char q[50]; it will work. Since you're only giving strcpy a pointer to a zero length string it doesn't have enough space to store the copied string and overwrites aka corrupts the memory.

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