a simple question regarding string copy

W

wahaha

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

Robert Gamble

wahaha said:
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?

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
 
W

wahaha

Robert said:
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.
 
K

Keith Thompson

wahaha said:
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?

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

David T. Ashley

Keith Thompson said:
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.

Also:

http://en.wikipedia.org/wiki/Memory_leak

Dave.
 
C

chat

I am not sure whether this code work well, for example, if some element
of b is zero before the last element is reach, then while(*a++ = *b++)
; will stop before copying the last element.
Is it true? I am not sure condition in while loop, it uses *a or a++
for exiting while loop?
chat watchara
 
R

Robert Gamble

chat said:
I am not sure whether this code work well, for example, if some element
of b is zero before the last element is reach, then while(*a++ = *b++)
; will stop before copying the last element.

The question was specifically about copying *strings* which by
definition are terminated by a null character (whose value is 0).
Anything past that wouldn't be part of the string and shouldn't be
copied.

Robert Gamble
 
B

Barry Schwarz

I am not sure whether this code work well, for example, if some element
of b is zero before the last element is reach, then while(*a++ = *b++)
; will stop before copying the last element.
Is it true? I am not sure condition in while loop, it uses *a or a++
for exiting while loop?
chat watchara

Whatever element of b contains the first 0 is BY DEFINITION the end of
the string.


Remove del for email
 
K

Keith Thompson

chat said:
I am not sure whether this code work well, for example, if some element
of b is zero before the last element is reach, then while(*a++ = *b++)
; will stop before copying the last element.
Is it true? I am not sure condition in while loop, it uses *a or a++
for exiting while loop?

What code? Please provide context when posting a followup. See
<http://cfaj.freeshell.org/google/>.

If you encounter a zero character ('\0'), that is by definition the
end of the string.
 
R

Richard Heathfield

chat said:
I am not sure whether this code work well, for example, if some element
of b is zero before the last element is reach, then while(*a++ = *b++)
; will stop before copying the last element.
Is it true?

No, if b is 0, it *is* the last element.
I am not sure condition in while loop, it uses *a or a++
for exiting while loop?

Neither. It uses the result of the expression *a++ = *b++

If you don't understand it, write a string-copying function that you *do*
understand.
 
D

Default User

Keith said:
What code? Please provide context when posting a followup. See
<http://cfaj.freeshell.org/google/>.

That's out of date. Google switched their interface so that the default
reply gives quotes now. There's no need to do the extra steps anymore.
To have no quotes, the OP had to specifically delete them.




Brian
 
S

santosh

Default said:
That's out of date. Google switched their interface so that the default
reply gives quotes now. There's no need to do the extra steps anymore.
To have no quotes, the OP had to specifically delete them.

It's possible, though unlikely, that one or more of the country
specific sub-sites of Google haven't yet been updated regarding this
change. I haven't tested any though.
 
K

Keith Thompson

Default User said:
That's out of date. Google switched their interface so that the default
reply gives quotes now. There's no need to do the extra steps anymore.
To have no quotes, the OP had to specifically delete them.

No, it's not out of date. If you read the web page, you'll see that
it acknowledges that Google has fixed its interface; it still has some
good information and links about usenet etiquette.
 
D

Default User

Keith said:
No, it's not out of date. If you read the web page, you'll see that
it acknowledges that Google has fixed its interface; it still has some
good information and links about usenet etiquette.


Ah yes, I see.




Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top