strcpy

Discussion in 'C++' started by Mike Mimic, May 16, 2004.

  1. Mike Mimic

    Mike Mimic Guest

    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
    Mike Mimic, May 16, 2004
    #1
    1. Advertising

  2. "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
    John Harrison, May 16, 2004
    #2
    1. Advertising

  3. 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.
    Simon Saunders, May 16, 2004
    #3
  4. Mike Mimic

    Julie Guest

    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));
    Julie, May 17, 2004
    #4
  5. "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
    John Harrison, May 17, 2004
    #5
  6. Mike Mimic

    Julie Guest

    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
    Julie, May 17, 2004
    #6
  7. "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
    Peter Koch Larsen, May 17, 2004
    #7
  8. Mike Mimic

    Julie Guest

    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
    Julie, May 17, 2004
    #8
  9. >
    > - 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
    John Harrison, May 17, 2004
    #9
  10. "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
    Peter Koch Larsen, May 17, 2004
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Sheer
    Replies:
    7
    Views:
    491
    Paul Sheer
    Sep 10, 2004
  2. Paul Sheer
    Replies:
    4
    Views:
    642
    Paul Sheer
    Sep 14, 2004
  3. RonHiler
    Replies:
    8
    Views:
    518
    John Harrison
    Oct 19, 2004
  4. Replies:
    11
    Views:
    928
    Alex Vinokur
    Jan 20, 2005
  5. strcpy

    , Jun 1, 2005, in forum: C++
    Replies:
    17
    Views:
    2,090
    sana1990
    Apr 21, 2010
Loading...

Share This Page