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. |
|