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. 14 1722
wahaha wrote:
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
Robert Gamble wrote:
wahaha wrote:
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.
I guess so. Thank you very much for your answer.
"wahaha" <zh***********@gmail.comwrites:
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.
--
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.
"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
"wahaha" <zh***********@gmail.comwrites:
>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.
Also: http://en.wikipedia.org/wiki/Memory_leak
Dave.
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
chat wrote:
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
On 4 Jan 2007 19:04:44 -0800, "chat" <ch***********@gmail.comwrote:
>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
"chat" <ch***********@gmail.comwrites:
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.
--
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.
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.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at the above domain, - www.
Richard Heathfield said:
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.
*b !!!
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at the above domain, - www.
Keith Thompson wrote:
"chat" <ch***********@gmail.comwrites:
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/>.
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
Default User wrote:
Keith Thompson wrote:
"chat" <ch***********@gmail.comwrites:
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/>.
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.
"Default User" <de***********@yahoo.comwrites:
Keith Thompson wrote:
>"chat" <ch***********@gmail.comwrites:
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/>.
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.
--
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.
Keith Thompson wrote:
"Default User" <de***********@yahoo.comwrites:
Keith Thompson wrote:
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: niclane |
last post by:
Hi,
I was reading section 5.5 of Ritchie and Kernighan and saw the
following:
"
.....
char amessage = "now is the time";
char *pmessage = "now is the time";
|
by: Ashwin |
last post by:
hi guys,
i have overloaded the << operator.as shown below.
ostream& operator<<(ostream &out, const student &a)
{
out<<a.idno;
out<< " " ;
// out<< a.name;
out<< " " ;
// out<< a.marks...
|
by: duffdevice |
last post by:
Hi, I came across this unexpected behavior while working on something
else. I am attempting to return a custom type by value from a global
function. I have a trace in the custom class's copy...
|
by: true911m |
last post by:
This is a simple walkthrough to get PyInstaller up and running.
I decided to give PI a try, because it claims to be more selective about what it bundles into its executable files by default, and...
|
by: Bjoern Schliessmann |
last post by:
Hello all,
I'm trying to simulate simple electric logic (asynchronous)
circuits. By "simple" I mean that I only want to know if I
have "current" or "no current" (it's quite digital) and the only...
|
by: somenath |
last post by:
Hi All,
I have a doubt regarding the pointer assignment . Please have a look
at the following program .
#include<stdio.h>
#include<stdlib.h>
#define NAMESIZE 10
#define SAFE_FREE(t) if(t)\...
|
by: somenath |
last post by:
Hi All,
I am trying to understand the behavior of the memcpy and memmove.
While doing so I wrote a program as mentioned bellow .
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
...
|
by: galiorenye |
last post by:
Hi,
Given this code:
A** ppA = new A*;
A *pA = NULL;
for(int i = 0; i < 10; ++i)
{
pA = ppA;
//do something with pA
|
by: Chris M. Thomasson |
last post by:
I use the following technique in all of my C++ projects; here is the example
code with error checking omitted for brevity:
_________________________________________________________________
/*...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |