String functions and overlapping objects

Discussion in 'C Programming' started by =?ISO-8859-15?Q?Jean=2DFran=E7ois?= Lemaire, Jul 30, 2007.

  1. Hello all,

    I'm learning C and I still am struggling to understand some basic
    concepts. For example, I read in the standard that with functions such
    as strcpy, 'If copying takes place between objects that overlap, the
    behavior is undefined.' But how can I be sure that they don't overlap?

    For example, if I write this:

    char string1[] = "overlap";
    char string2[8];
    strcpy(string2, string1);

    string1 and string2 can't possibly overlap, can they? On the other hand,
    I understand that if string2 was declared as:

    char *string2;

    I would be in trouble. But my main concern wouldn't be overlapping,
    would it? That's where I'm confused.

    I hope I'm making some sense.

    JFL
    =?ISO-8859-15?Q?Jean=2DFran=E7ois?= Lemaire, Jul 30, 2007
    #1
    1. Advertising

  2. =?ISO-8859-15?Q?Jean=2DFran=E7ois?= Lemaire

    Default User Guest

    Jean-François Lemaire wrote:

    > Hello all,
    >
    > I'm learning C and I still am struggling to understand some basic
    > concepts. For example, I read in the standard that with functions such
    > as strcpy, 'If copying takes place between objects that overlap, the
    > behavior is undefined.' But how can I be sure that they don't overlap?
    >
    > For example, if I write this:
    >
    > char string1[] = "overlap";
    > char string2[8];
    > strcpy(string2, string1);
    >
    > string1 and string2 can't possibly overlap, can they?


    That is correct, they don't overlap. An overlapping situation could be
    something like:

    char str[] = "Hello";

    strcpy(str, str+1);


    Someone might do that trying to remove the first character.


    > On the other
    > hand, I understand that if string2 was declared as:
    >
    > char *string2;
    >
    > I would be in trouble. But my main concern wouldn't be overlapping,
    > would it? That's where I'm confused.


    That's not a string, it's a pointer to char. If it were an automatic
    variable, its value would be indeterminant.




    Brian
    Default User, Jul 30, 2007
    #2
    1. Advertising

  3. =?ISO-8859-15?Q?Jean=2DFran=E7ois?= Lemaire

    santosh Guest

    Jean-François Lemaire wrote:

    > Hello all,
    >
    > I'm learning C and I still am struggling to understand some basic
    > concepts. For example, I read in the standard that with functions such
    > as strcpy, 'If copying takes place between objects that overlap, the
    > behavior is undefined.' But how can I be sure that they don't overlap?


    By keeping track of your pointer values.

    > For example, if I write this:
    >
    > char string1[] = "overlap";
    > char string2[8];
    > strcpy(string2, string1);
    >
    > string1 and string2 can't possibly overlap, can they?


    No, because they are separate objects. Overlapping is possible, within
    Standard C, only between objects which are parts of a larger object.

    > On the other hand,
    > I understand that if string2 was declared as:
    >
    > char *string2;
    >
    > I would be in trouble.


    As long as you initialise string2 to point to valid storage of sufficient
    size, there would be no trouble.

    > But my main concern wouldn't be overlapping,
    > would it? That's where I'm confused.
    >
    > I hope I'm making some sense.


    An example of overlapping strings:

    char msg[] = "hello, world!";
    char *ptr = &msg[6];

    Since ptr and msg both point to different regions of the same array, you
    need to be careful not pass ptr and msg to functions that cannot deal with
    overlapping objects.
    santosh, Jul 30, 2007
    #3
  4. On Monday 30 July 2007 10:57, santosh wrote:

    > Jean-François Lemaire wrote:


    >> I'm learning C and I still am struggling to understand some basic
    >> concepts. For example, I read in the standard that with functions
    >> such as strcpy, 'If copying takes place between objects that overlap,
    >> the behavior is undefined.'


    >> For example, if I write this:
    >>
    >> char string1[] = "overlap";
    >> char string2[8];
    >> strcpy(string2, string1);
    >>
    >> string1 and string2 can't possibly overlap, can they?

    >
    > No, because they are separate objects. Overlapping is possible, within
    > Standard C, only between objects which are parts of a larger object.


    OK. That's where I was lost. I hadn't taken into account the fact that
    two objects can be part of another object.

    > An example of overlapping strings:
    >
    > char msg[] = "hello, world!";
    > char *ptr = &msg[6];
    >
    > Since ptr and msg both point to different regions of the same array,
    > you need to be careful not pass ptr and msg to functions that cannot
    > deal with overlapping objects.


    OK. This example and you explanation makes it perfectly clear for me
    now. Thanks for you time.

    JFL
    =?ISO-8859-15?Q?Jean=2DFran=E7ois?= Lemaire, Jul 30, 2007
    #4
  5. On Mon, 30 Jul 2007 14:27:11 +0530, santosh <>
    wrote:

    > Jean-François Lemaire wrote:


    > > char string1[] = "overlap";
    > > char string2[8];
    > > strcpy(string2, string1);
    > >
    > > string1 and string2 can't possibly overlap, can they?

    >
    > No, because they are separate objects. Overlapping is possible, within
    > Standard C, only between objects which are parts of a larger object.
    >

    Nit: string literals -- or more precisely the runtime string objects
    created by string literals in a context other than as initializer for
    a char or wchar_t array -- can share storage including overlap. E.g.:
    char * a = "foobar";
    char * b = "bar";
    It is permitted that b == &a[3] but not required. In the old days when
    memory wasn't cheaper than dirt*, some compilers actually bothered to
    do this. A similar point applies in C99 to const-qualified compound
    literals. (* It used to be "AS cheap AS dirt", but computer memory has
    since gotten cheaper while dirt has gotten more expensive. <G>)

    However, all AFAICR of the places where standard library routines are
    unsafe for overlap are where (at least) one of them is written to, and
    writing to string literals or const compound literals is illegal
    always, without even thinking about overlap.

    <snip rest>

    - formerly david.thompson1 || achar(64) || worldnet.att.net
    David Thompson, Aug 26, 2007
    #5
    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. Tony
    Replies:
    4
    Views:
    2,137
    Andy De Petter
    Nov 27, 2003
  2. mike
    Replies:
    7
    Views:
    601
    mikeotp
    Feb 4, 2005
  3. Xiangliang Meng
    Replies:
    1
    Views:
    1,590
    Victor Bazarov
    Jun 21, 2004
  4. 7stud
    Replies:
    11
    Views:
    682
    Dennis Lee Bieber
    Mar 20, 2007
  5. Ardanwen
    Replies:
    5
    Views:
    119
    Ardanwen
    Dec 16, 2004
Loading...

Share This Page