String functions and overlapping objects

  • Thread starter =?ISO-8859-15?Q?Jean=2DFran=E7ois?= Lemaire
  • Start date
?

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

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
 
D

Default User

Jean-François Lemaire said:
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
 
S

santosh

Jean-François Lemaire said:
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.
 
?

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

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
 
D

David Thompson

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
 

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,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top