strcpy

M

Mike Mimic

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
 
J

John Harrison

Mike Mimic said:
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
 
S

Simon Saunders

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

Julie

John said:
Mike Mimic said:
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));
 
J

John Harrison

Julie said:
John said:
Mike Mimic said:
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
 
J

Julie

John said:
Julie said:
John said:
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
 
P

Peter Koch Larsen

Julie said:
John said:
Julie said:
John Harrison 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:

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
 
J

Julie

Peter said:
And - as John said - they are exactly the same.

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

John Harrison

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

Peter Koch Larsen

Julie said:
Peter Koch Larsen wrote: [snip9
And - as John said - they are exactly the same. other
mbcs),

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.
 

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,054
Latest member
TrimKetoBoost

Latest Threads

Top