By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,245 Members | 1,216 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,245 IT Pros & Developers. It's quick & easy.

String functions and overlapping objects

P: n/a
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
Jul 30 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
Jul 30 '07 #2

P: n/a
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.

Jul 30 '07 #3

P: n/a
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
Jul 30 '07 #4

P: n/a
On Mon, 30 Jul 2007 14:27:11 +0530, santosh <sa*********@gmail.com>
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
Aug 26 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.