由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - a simple question regarding string copy in C
相关主题
请版上的C++牛人讲一下g++的优化参数is smart_ptr really that good?
Pointer to iterator?[合集] pointer in C
菜鸟请教smart pointerpointer overflow
[转载]有关编程语言的比较:Succinctness is powerabout STL functor and function pointers
Why do I need to use "plain" pointer?what's the purpose of pointer to pointers?
一个简单的小问题C++ Q05: pointer to constant variable
请问可以这样定义struct吗?find start point of loop from linked list
how to do it ?=======Problem – coding in c++C++ Q93 - Q95 (转载)
相关话题的讨论汇总
话题: string话题: copy话题: cause话题: code话题: question
进入Programming版参与讨论
1 (共1页)
d********i
发帖数: 8
1
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.
t****t
发帖数: 6806
2
if the memory pointed by a is properly allocated, it will not cause leak or
anything bad.

【在 d********i 的大作中提到】
: 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.

w*c
发帖数: 34
3
1. your question has nothing to do with memory leak
2. there are too many dangerous assumptions in your code.
1 (共1页)
进入Programming版参与讨论
相关主题
C++ Q93 - Q95 (转载)Why do I need to use "plain" pointer?
c++ pointers are iterators, why?一个简单的小问题
int F::*x = &F::x是什么意思?请问可以这样定义struct吗?
c++ 里用到pointer 的地方我们尽可能用smart pointer吗? how to do it ?=======Problem – coding in c++
请版上的C++牛人讲一下g++的优化参数is smart_ptr really that good?
Pointer to iterator?[合集] pointer in C
菜鸟请教smart pointerpointer overflow
[转载]有关编程语言的比较:Succinctness is powerabout STL functor and function pointers
相关话题的讨论汇总
话题: string话题: copy话题: cause话题: code话题: question