strlen runtime error after call strcpy

D

Duke

#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.
 
D

Duke

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
 
R

Richard Heathfield

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.
 
R

Richard Heathfield

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.
 
C

Clever Monkey

Richard said:
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.
 
D

Default User

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);
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
 
M

Mark McIntyre

#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
 
C

CBFalconer

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);

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
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top