Redian新闻
>
a simple question regarding string copy in C
avatar
a simple question regarding string copy in C# Programming - 葵花宝典
d*i
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.
avatar
t*t
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.

avatar
w*c
3
1. your question has nothing to do with memory leak
2. there are too many dangerous assumptions in your code.
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。