Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > strcpy

Reply
Thread Tools

strcpy

 
 
Mike Mimic
Guest
Posts: n/a
 
      05-16-2004
Hi!

Is it possible to write this (and that it will work):

char *buffer = ... // some array of chars terminated by \0 (allocated

// with new and filled somewhere else)
int t = ... // some value - how much to delete at the begining

char *rest = new char[strlen(buffer) - t + 1];
strcpy(rest, buffer + t);
delete[] buffer;
buffer = rest;

in this way:

char *buffer = ... // some array of chars terminated by \0 (allocated

// with new and filled somewhere else)
int t = ... // some value - how much to delete at the begining

strcpy(buffer, buffer + t);

(This is only a code simmilar to that in my program but the idea is
here, t is always smaller than length.)


Mike
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      05-16-2004

"Mike Mimic" <> wrote in message
news:c87itd$2cp$...
> Hi!
>
> Is it possible to write this (and that it will work):
>
> char *buffer = ... // some array of chars terminated by \0 (allocated
>
> // with new and filled somewhere else)
> int t = ... // some value - how much to delete at the begining
>
> char *rest = new char[strlen(buffer) - t + 1];
> strcpy(rest, buffer + t);
> delete[] buffer;
> buffer = rest;
>
> in this way:
>
> char *buffer = ... // some array of chars terminated by \0 (allocated
>
> // with new and filled somewhere else)
> int t = ... // some value - how much to delete at the begining
>
> strcpy(buffer, buffer + t);
>


No, the behaviour of strcpy is undefined if the source and destination
strings overlap.

You can use memmove instead which is guaranteed to work even for overlapping
memory blocks

memmove(buffer, buffer + t, strlen(buffer + t) + 1);

john


 
Reply With Quote
 
 
 
 
Simon Saunders
Guest
Posts: n/a
 
      05-16-2004
On Sun, 16 May 2004 13:22:25 +0200, Mike Mimic wrote:

> Hi!
>
> Is it possible to write this (and that it will work):
>
> char *buffer = ... // some array of chars terminated by \0 (allocated
>
> // with new and filled somewhere else)
> int t = ... // some value - how much to delete at the begining
>
> char *rest = new char[strlen(buffer) - t + 1];
> strcpy(rest, buffer + t);
> delete[] buffer;
> buffer = rest;
>
> in this way:
>
> strcpy(buffer, buffer + t);
>


strcpy isn't required to work properly when the source and destination
overlap; for that case you need memmove.

 
Reply With Quote
 
Julie
Guest
Posts: n/a
 
      05-17-2004
John Harrison wrote:
>
> "Mike Mimic" <> wrote in message
> news:c87itd$2cp$...
> > Hi!
> >
> > Is it possible to write this (and that it will work):
> >
> > char *buffer = ... // some array of chars terminated by \0 (allocated
> >
> > // with new and filled somewhere else)
> > int t = ... // some value - how much to delete at the begining
> >
> > char *rest = new char[strlen(buffer) - t + 1];
> > strcpy(rest, buffer + t);
> > delete[] buffer;
> > buffer = rest;
> >
> > in this way:
> >
> > char *buffer = ... // some array of chars terminated by \0 (allocated
> >
> > // with new and filled somewhere else)
> > int t = ... // some value - how much to delete at the begining
> >
> > strcpy(buffer, buffer + t);
> >

>
> No, the behaviour of strcpy is undefined if the source and destination
> strings overlap.
>
> You can use memmove instead which is guaranteed to work even for overlapping
> memory blocks
>
> memmove(buffer, buffer + t, strlen(buffer + t) + 1);
>
> john


Should be:

memmove(buffer, buffer + t, (strlen(buffer+t)+1)*sizeof(*buffer));
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      05-17-2004

"Julie" <> wrote in message
news:...
> John Harrison wrote:
> >
> > "Mike Mimic" <> wrote in message
> > news:c87itd$2cp$...
> > > Hi!
> > >
> > > Is it possible to write this (and that it will work):
> > >
> > > char *buffer = ... // some array of chars terminated by \0 (allocated
> > >
> > > // with new and filled somewhere else)
> > > int t = ... // some value - how much to delete at the begining
> > >
> > > char *rest = new char[strlen(buffer) - t + 1];
> > > strcpy(rest, buffer + t);
> > > delete[] buffer;
> > > buffer = rest;
> > >
> > > in this way:
> > >
> > > char *buffer = ... // some array of chars terminated by \0 (allocated
> > >
> > > // with new and filled somewhere else)
> > > int t = ... // some value - how much to delete at the begining
> > >
> > > strcpy(buffer, buffer + t);
> > >

> >
> > No, the behaviour of strcpy is undefined if the source and destination
> > strings overlap.
> >
> > You can use memmove instead which is guaranteed to work even for

overlapping
> > memory blocks
> >
> > memmove(buffer, buffer + t, strlen(buffer + t) + 1);
> >
> > john

>
> Should be:
>
> memmove(buffer, buffer + t, (strlen(buffer+t)+1)*sizeof(*buffer));


Why? sizeof(*buffer) is guaranteed to be one. Or are you making a stylistic
comment?

john


 
Reply With Quote
 
Julie
Guest
Posts: n/a
 
      05-17-2004
John Harrison wrote:
>
> "Julie" <> wrote in message
> news:...
> > John Harrison wrote:
> > >
> > > "Mike Mimic" <> wrote in message
> > > news:c87itd$2cp$...
> > > > Hi!
> > > >
> > > > Is it possible to write this (and that it will work):
> > > >
> > > > char *buffer = ... // some array of chars terminated by \0 (allocated
> > > >
> > > > // with new and filled somewhere else)
> > > > int t = ... // some value - how much to delete at the begining
> > > >
> > > > char *rest = new char[strlen(buffer) - t + 1];
> > > > strcpy(rest, buffer + t);
> > > > delete[] buffer;
> > > > buffer = rest;
> > > >
> > > > in this way:
> > > >
> > > > char *buffer = ... // some array of chars terminated by \0 (allocated
> > > >
> > > > // with new and filled somewhere else)
> > > > int t = ... // some value - how much to delete at the begining
> > > >
> > > > strcpy(buffer, buffer + t);
> > > >
> > >
> > > No, the behaviour of strcpy is undefined if the source and destination
> > > strings overlap.
> > >
> > > You can use memmove instead which is guaranteed to work even for

> overlapping
> > > memory blocks
> > >
> > > memmove(buffer, buffer + t, strlen(buffer + t) + 1);
> > >
> > > john

> >
> > Should be:
> >
> > memmove(buffer, buffer + t, (strlen(buffer+t)+1)*sizeof(*buffer));

>
> Why? sizeof(*buffer) is guaranteed to be one. Or are you making a stylistic
> comment?
>
> john


No, stylistic comment, but:

- strlen returns a character count, memmove takes a byte count, multiplying
simply converts from char count to bytes.

- More importantly, if the user changes from ASCII to Unicode (or other mbcs),
then their code is much more portable/convertible.

In this day and age, it is no longer safe to make assumptions that
sizeof('character type') == 1
 
Reply With Quote
 
Peter Koch Larsen
Guest
Posts: n/a
 
      05-17-2004

"Julie" <> skrev i en meddelelse
news:...
> John Harrison wrote:
> >
> > "Julie" <> wrote in message
> > news:...
> > > John Harrison wrote:
> > > >
> > > > "Mike Mimic" <> wrote in message
> > > > news:c87itd$2cp$...
> > > > > Hi!
> > > > >
> > > > > Is it possible to write this (and that it will work):
> > > > >
> > > > > char *buffer = ... // some array of chars terminated by \0

(allocated
> > > > >
> > > > > // with new and filled somewhere else)
> > > > > int t = ... // some value - how much to delete at the begining
> > > > >
> > > > > char *rest = new char[strlen(buffer) - t + 1];
> > > > > strcpy(rest, buffer + t);
> > > > > delete[] buffer;
> > > > > buffer = rest;
> > > > >
> > > > > in this way:
> > > > >
> > > > > char *buffer = ... // some array of chars terminated by \0

(allocated
> > > > >
> > > > > // with new and filled somewhere else)
> > > > > int t = ... // some value - how much to delete at the begining
> > > > >
> > > > > strcpy(buffer, buffer + t);
> > > > >
> > > >
> > > > No, the behaviour of strcpy is undefined if the source and

destination
> > > > strings overlap.
> > > >
> > > > You can use memmove instead which is guaranteed to work even for

> > overlapping
> > > > memory blocks
> > > >
> > > > memmove(buffer, buffer + t, strlen(buffer + t) + 1);
> > > >
> > > > john
> > >
> > > Should be:
> > >
> > > memmove(buffer, buffer + t, (strlen(buffer+t)+1)*sizeof(*buffer));

> >
> > Why? sizeof(*buffer) is guaranteed to be one. Or are you making a

stylistic
> > comment?
> >
> > john

>
> No, stylistic comment, but:
>
> - strlen returns a character count, memmove takes a byte count,

multiplying
> simply converts from char count to bytes.


And - as John said - they are exactly the same.
>
> - More importantly, if the user changes from ASCII to Unicode (or other

mbcs),
> then their code is much more portable/convertible.


Well... that could be argued. The code probably deserves a major review if
not using plain chars.

>
> In this day and age, it is no longer safe to make assumptions that
> sizeof('character type') == 1


/Peter


 
Reply With Quote
 
Julie
Guest
Posts: n/a
 
      05-17-2004
Peter Koch Larsen wrote:
> > > > > You can use memmove instead which is guaranteed to work even for
> > > overlapping
> > > > > memory blocks
> > > > >
> > > > > memmove(buffer, buffer + t, strlen(buffer + t) + 1);
> > > > >
> > > > > john
> > > >
> > > > Should be:
> > > >
> > > > memmove(buffer, buffer + t, (strlen(buffer+t)+1)*sizeof(*buffer));
> > >
> > > Why? sizeof(*buffer) is guaranteed to be one. Or are you making a

> stylistic
> > > comment?
> > >
> > > john

> >
> > No, stylistic comment, but:
> >
> > - strlen returns a character count, memmove takes a byte count,

> multiplying
> > simply converts from char count to bytes.

>
> And - as John said - they are exactly the same.
> >
> > - More importantly, if the user changes from ASCII to Unicode (or other

> mbcs),
> > then their code is much more portable/convertible.

>
> Well... that could be argued. The code probably deserves a major review if
> not using plain chars.


No disagreement for changes to existing code -- it should be reviewed in
detail.

However, any new code should be built to be as portable to non _char_ character
types. Duty now for the future.


> >
> > In this day and age, it is no longer safe to make assumptions that
> > sizeof('character type') == 1

>
> /Peter

 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      05-17-2004
>
> - More importantly, if the user changes from ASCII to Unicode (or other

mbcs),
> then their code is much more portable/convertible.
>
> In this day and age, it is no longer safe to make assumptions that
> sizeof('character type') == 1


If the change is made from ASCII to Unicode then the call to strlen would
also need to change to wcslen. I'd hope sizeof would be added at the same
time.

I'm not disagreeing with you, but I don't think I'd be bothered with sizeof,
if I wrote the code.

john


 
Reply With Quote
 
Peter Koch Larsen
Guest
Posts: n/a
 
      05-17-2004

"Julie" <> skrev i en meddelelse
news:...
> Peter Koch Larsen wrote:

[snip9
> > > > > memmove(buffer, buffer + t, (strlen(buffer+t)+1)*sizeof(*buffer));
> > > >
> > > > Why? sizeof(*buffer) is guaranteed to be one. Or are you making a

> > stylistic
> > > > comment?
> > > >
> > > > john
> > >
> > > No, stylistic comment, but:
> > >
> > > - strlen returns a character count, memmove takes a byte count,

> > multiplying
> > > simply converts from char count to bytes.

> >
> > And - as John said - they are exactly the same.
> > >
> > > - More importantly, if the user changes from ASCII to Unicode (or

other
> > mbcs),
> > > then their code is much more portable/convertible.

> >
> > Well... that could be argued. The code probably deserves a major review

if
> > not using plain chars.

>
> No disagreement for changes to existing code -- it should be reviewed in
> detail.
>
> However, any new code should be built to be as portable to non _char_

character
> types. Duty now for the future.


Of course. But the code shown was not in any way in a condition that you
might consider porting it anyway.

>
>
> > >
> > > In this day and age, it is no longer safe to make assumptions that
> > > sizeof('character type') == 1

> >
> > /Peter



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
strcpy GrahamJWalsh@gmail.com C++ 17 04-21-2010 06:40 AM
strcpy of a class object leonkatz@gmail.com C++ 11 01-20-2005 04:44 AM
strcpy and the copy constructor RonHiler C++ 8 10-19-2004 06:30 AM
C++ Compiler with a -Wwarn-use-of-strcpy or similar option?? Paul Sheer C++ 4 09-14-2004 08:38 PM
C++ Compiler with a -Wwarn-use-of-strcpy or similar option?? Paul Sheer C++ 7 09-10-2004 05:07 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57