www.digitalmars.com         C & C++   DMDScript  

D - overlapping array copy

reply "Carlos" <carlos8294 msn.com> writes:
If strings have to be read like this:

char [] str;
char [80] tmp;
scanf("%s",tmp);
str=tmp[0..strlen(tmp)];

why doesn't it work?:

void inputString(inout char[] a)
{
    char [80] w;
    fflush(stdin);
    printf("%.*s",mensaje);
    scanf("%s",w);
    a=w[0..strlen(w)];
}

It can be compiled, and the program actually runs, but AFTER it reads the
string, this message is shown:

Error: overlapping array copy

and doesn't say anything else.
May 26 2002
next sibling parent "Carlos" <carlos8294 msn.com> writes:
Just realized of something:

 If strings have to be read like this:

 char [] str;
 char [80] tmp;
 scanf("%s",tmp);
 str=tmp[0..strlen(tmp)];

 why doesn't it work?:

 void inputString(inout char[] a)
 {
     char [80] w;
     fflush(stdin);
     printf("%.*s",mensaje);

this line (printf...) doesn't belong to this function. sorry
     scanf("%s",w);
     a=w[0..strlen(w)];
 }

 It can be compiled, and the program actually runs, but AFTER it reads the
 string, this message is shown:

 Error: overlapping array copy

 and doesn't say anything else.

May 27 2002
prev sibling parent reply "Walter" <walter digitalmars.com> writes:
The trouble here is that scanf() expects a pointer to a string, not a
dynamic array. To correct the problem, use:

    scanf("%s", (char *)w);

"Carlos" <carlos8294 msn.com> wrote in message
news:acs4dc$1sff$1 digitaldaemon.com...
 why doesn't it work?:

 void inputString(inout char[] a)
 {
     char [80] w;
     fflush(stdin);
     printf("%.*s",mensaje);
     scanf("%s",w);
     a=w[0..strlen(w)];
 }

 It can be compiled, and the program actually runs, but AFTER it reads the
 string, this message is shown:

 Error: overlapping array copy

 and doesn't say anything else.

May 28 2002
parent reply "Pavel Minayev" <evilone omen.ru> writes:
"Walter" <walter digitalmars.com> wrote in message
news:ad0vi8$g7s$1 digitaldaemon.com...

 The trouble here is that scanf() expects a pointer to a string, not a
 dynamic array. To correct the problem, use:

     scanf("%s", (char *)w);

Or: char[] w; stdin.scanf("%.*s", w);
May 28 2002
parent reply "Sandor Hojtsy" <hojtsy index.hu> writes:
"Pavel Minayev" <evilone omen.ru> wrote in message
news:ad1qm5$2h26$1 digitaldaemon.com...
 The trouble here is that scanf() expects a pointer to a string, not a
 dynamic array. To correct the problem, use:

     scanf("%s", (char *)w);

char[] w; stdin.scanf("%.*s", w);

Who will allocate the memory for the characters in Pavel's version? What happens on buffer overflow in Walter's version? Sandor
May 29 2002
parent "Pavel Minayev" <evilone omen.ru> writes:
"Sandor Hojtsy" <hojtsy index.hu> wrote in message
news:ad2nbk$kv6$1 digitaldaemon.com...

 news:ad1qm5$2h26$1 digitaldaemon.com...
 The trouble here is that scanf() expects a pointer to a string, not a
 dynamic array. To correct the problem, use:

     scanf("%s", (char *)w);

char[] w; stdin.scanf("%.*s", w);

Who will allocate the memory for the characters in Pavel's version?

Stream::scanf() will. When it sees "%.*s", it automatically allocates the amount of bytes required to store the string read. You don't have to worry about it at all.
 What happens on buffer overflow in Walter's version?

GPF, if you are lucky. Otherwise, some garbage will be written into the memory (which could be the placeholder for another array you've declared).
May 29 2002