关于字符串复制的简单问题 [英] a simple question regarding string copy

查看:69
本文介绍了关于字符串复制的简单问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




在一个小代码中实现字符串副本:


while(* a ++ = * b ++);


a和b都是char指针。这段代码可以工作但最后,当* a

= * b =''\ 0''结束时,a和b都会超越最后一个

字符串的字符是\0"因为++操作。

这是否会导致内存泄漏?


我已经用GCC测试了这段代码,它有效但是这个简洁

编码会导致问题吗?


谢谢。

Hi,

In a small code to implement string copy:

while(*a++ = *b++) ;

both a and b are char pointers. This code works but at the end, when *a
= *b = ''\0'' finishes, both a and b step one block beyond the last
character of the string which is "\0" because of the "++" operation.
Will this cause memory leak?

I have tested this code with GCC, it works but will this succinct
coding cause problem later?

Thank you.

推荐答案

wahaha写道:
wahaha wrote:




在一个小代码中实现字符串副本:


while (* a ++ = * b ++);


a和b都是char指针。这段代码可以工作但最后,当* a

= * b =''\ 0''结束时,a和b都会超越最后一个

字符串的字符是\0"因为++操作。

这会导致内存泄漏吗?
Hi,

In a small code to implement string copy:

while(*a++ = *b++) ;

both a and b are char pointers. This code works but at the end, when *a
= *b = ''\0'' finishes, both a and b step one block beyond the last
character of the string which is "\0" because of the "++" operation.
Will this cause memory leak?



在C中合法创建一个指针,指向一个数组的最后一个

元素,只要你不要然后取消引用指针

(你的代码不是)。我不确定为什么你会看到内存泄漏的机会,但你提供的代码是一个经典的

strcpy实现示例。


Robert Gamble

It is legal in C to create a pointer that points to one past the last
element of an array as long as you don''t then dereference the pointer
(which your code doesn''t). I''m not sure why you might see an
opportunity for a memory leak but the code you presented is a classic
strcpy implementation example.

Robert Gamble




Robert Gamble写道:

Robert Gamble wrote:

wahaha写道:
wahaha wrote:




在一个实现字符串副本的小代码中:


while(* a ++ = * b ++);


a和b都是char指针。这段代码可以工作但最后,当* a

= * b =''\ 0''结束时,a和b都会超越最后一个

字符串的字符是\0"因为++操作。

这会导致内存泄漏吗?
Hi,

In a small code to implement string copy:

while(*a++ = *b++) ;

both a and b are char pointers. This code works but at the end, when *a
= *b = ''\0'' finishes, both a and b step one block beyond the last
character of the string which is "\0" because of the "++" operation.
Will this cause memory leak?



在C中合法创建一个指针,指向一个数组的最后一个

元素,只要你不要然后取消引用指针

(你的代码不是)。我不确定为什么你会看到内存泄漏的机会,但你提供的代码是一个经典的

strcpy实现示例。


It is legal in C to create a pointer that points to one past the last
element of an array as long as you don''t then dereference the pointer
(which your code doesn''t). I''m not sure why you might see an
opportunity for a memory leak but the code you presented is a classic
strcpy implementation example.



我想是的。非常感谢您的回答。

I guess so. Thank you very much for your answer.


" wahaha" < zh *********** @ gmail.comwrites:
"wahaha" <zh***********@gmail.comwrites:

在一个实现字符串副本的小代码中:


while(* a ++ = * b ++);


a和b都是char指针。这段代码可以工作但最后,当* a

= * b =''\ 0''结束时,a和b都会超越最后一个

字符串的字符是\0"因为++操作。

这是否会导致内存泄漏?


我已经用GCC测试了这段代码,它有效但是这个简洁

编码后来导致问题?
In a small code to implement string copy:

while(*a++ = *b++) ;

both a and b are char pointers. This code works but at the end, when *a
= *b = ''\0'' finishes, both a and b step one block beyond the last
character of the string which is "\0" because of the "++" operation.
Will this cause memory leak?

I have tested this code with GCC, it works but will this succinct
coding cause problem later?



内存泄漏仅在您分配内存时(例如,通过调用

malloc())并且无法解除分配(例如,通过调用free())。由于你的代码片段既没有分配也没有释放任何内存,它就不会泄漏内存。


可以想象它是*引起内存泄漏的原因。例如,如果

b指向malloc()ed内存块的开头,并且你在修改它之前没有保存其他地方的价值
,b已被更改的事实意味着你已经无法通过内存(并且如果你试图传递b的修改值)来获得
free()

free(),你会调用未定义的行为)。所以不要这样做。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *< http:// users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。

A "memory leak" occurs only when you allocate memory (e.g., by calling
malloc()) and fail to deallocate it (e.g., by calling free()). Since
your code fragment neither allocates nor deallocates any memory, it
cannot leak memory.

It could conceivably be the *cause* of a memory leak. For example, if
b points to the beginning of a malloc()ed block of memory, and you
haven''t saved the value of be somewhere else before modifying it, the
fact that b has been changed means that you no longer have a way to
free() the memory (and if you try to pass the modified value of b to
free(), you''ll invoke undefined behavior). So don''t do that.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


这篇关于关于字符串复制的简单问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆