Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > strlen runtime error after call strcpy

Reply
Thread Tools

strlen runtime error after call strcpy

 
 
Duke
Guest
Posts: n/a
 
      04-18-2007
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
char *s = "hello strlen";
printf("%s has %d chars.\n", s, strlen(s));
//the above strlen function execute correctly
char *msg1 = "abcdefghijklmnopqrstuvwxyz";

char buf[10];

strcpy(buf, msg1);
printf("[%s] length: %d\n", msg1, strlen(msg1));
//but the above statement will throw a runtime os exception
system("PAUSE");
return 0;
}

I don't know what occus after I find all the resource about c which I
can find.

 
Reply With Quote
 
 
 
 
Duke
Guest
Posts: n/a
 
      04-18-2007
PS: I found when I change the definition of buf from 'char buf[10]' to
'char *buf', then it execute correctly.

I don't know why? What dissimilitude char array and the char pointer


 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      04-18-2007
Duke said:

> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> int main(int argc, char *argv[])
> {
> char *s = "hello strlen";
> printf("%s has %d chars.\n", s, strlen(s));
> //the above strlen function execute correctly
> char *msg1 = "abcdefghijklmnopqrstuvwxyz";
>
> char buf[10];
>
> strcpy(buf, msg1);


buf is an array of 10 characters. Since a string is a sequence of
characters terminated by the first null character, it follows that buf
has sufficient storage to store a string of at most nine non-null
characters. Assuming that's the whole alphabet you have msg1 pointing
to (I didn't check carefully), you will require 27 bytes of storage in
buf - the 10 is just insufficient.

Once you trash your buffer in this way, the subsequent behaviour of the
program is undefined.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      04-18-2007
Duke said:

> PS: I found when I change the definition of buf from 'char buf[10]' to
> 'char *buf', then it execute correctly.


No, it doesn't. It just fails to break in quite the same way. In this
case, it's broken in a way that you don't happen to notice at the
moment.

> I don't know why? What dissimilitude char array and the char pointer


An array is a place in which to keep things. A pointer is a signpost,
for showing how to get to things. You can point a signpost at a city,
but you can't store a city in a signpost.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
Clever Monkey
Guest
Posts: n/a
 
      04-18-2007
Richard Heathfield wrote:
> Duke said:
>
>> PS: I found when I change the definition of buf from 'char buf[10]' to
>> 'char *buf', then it execute correctly.

>
> No, it doesn't. It just fails to break in quite the same way. In this
> case, it's broken in a way that you don't happen to notice at the
> moment.
>
>> I don't know why? What dissimilitude char array and the char pointer

>
> An array is a place in which to keep things. A pointer is a signpost,
> for showing how to get to things. You can point a signpost at a city,
> but you can't store a city in a signpost.
>

Nice analogy.
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      04-18-2007
Duke wrote:

> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> int main(int argc, char *argv[])
> {
> char *s = "hello strlen";
> printf("%s has %d chars.\n", s, strlen(s));
> //the above strlen function execute correctly
> char *msg1 = "abcdefghijklmnopqrstuvwxyz";
>
> char buf[10];
>
> strcpy(buf, msg1);
> printf("[%s] length: %d\n", msg1, strlen(msg1));
> //but the above statement will throw a runtime os exception
> system("PAUSE");
> return 0;
> }
>
> I don't know what occus after I find all the resource about c which I
> can find.



What exactly are you trying to accomplish here? Either you don't know
how to work strings, or you're deliberately trying broken code to see
what happens.

If the former, read over your text or FAQ sections dealing with
strings. If the latter, stop. It tells you very little, and wastes
everybody's time. There is no defined behavior for Undefined Behavior.




Brian
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      04-18-2007
On 18 Apr 2007 09:59:27 -0700, in comp.lang.c , Duke
<> wrote:

>#include <stdio.h>
>#include <stdlib.h>
>#include <string.h>
>
>int main(int argc, char *argv[])
>{
> char *s = "hello strlen";
> printf("%s has %d chars.\n", s, strlen(s));
> //the above strlen function execute correctly
> char *msg1 = "abcdefghijklmnopqrstuvwxyz";
>
> char buf[10];
>
> strcpy(buf, msg1);


Error - you just copied 25 or so characters into a space that can only
hold ten. The memory used by your programme is now corrupted, and
anything could happen....

> printf("[%s] length: %d\n", msg1, strlen(msg1));
> //but the above statement will throw a runtime os exception


..... including a runtime exception

Fix: don't try to overfill things.

For comparison, what happens if you try to put a five gallons of beer
into a human? It overflows, probably exceptionally...
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      04-18-2007
Duke wrote:
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> int main(int argc, char *argv[])
> {
> char *s = "hello strlen";
> printf("%s has %d chars.\n", s, strlen(s));
> //the above strlen function execute correctly
> char *msg1 = "abcdefghijklmnopqrstuvwxyz";
>
> char buf[10];
>
> strcpy(buf, msg1);


Your program has involved undefined behaviour here. buf is not
large enough. In addition, unless you have a C99 compiler, the
declaration of buf is invalid. Move it up after the declaration of
s.

> printf("[%s] length: %d\n", msg1, strlen(msg1));
> //but the above statement will throw a runtime os exception
> system("PAUSE");


This may or may not do anything.

> return 0;
> }
>
> I don't know what occus after I find all the resource about c which I
> can find.


Also, without a C99 compiler, the // comments may be illegal.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
strlen, strcmp and strcpy functions coinjo C++ 4 03-01-2006 05:22 AM
error C2664: 'strcpy' : cannot convert parameter 2 from 'char' to 'const char *' kaizen C++ 3 01-21-2006 08:07 PM
Two Questions about "strlen", "strcat" and "strcpy" Matt C Programming 82 09-29-2004 12:26 PM
Bad File Descriptor Error on strcat/strcpy lynology C Programming 4 08-18-2004 04:06 PM
strlen Imran C++ 2 08-12-2004 04:36 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57