473,378 Members | 1,413 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

strlen runtime error after call strcpy

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

Apr 18 '07 #1
7 2872
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
Apr 18 '07 #2
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.
Apr 18 '07 #3
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.
Apr 18 '07 #4
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.
Apr 18 '07 #5
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
Apr 18 '07 #6
On 18 Apr 2007 09:59:27 -0700, in comp.lang.c , Duke
<xa****@gmail.comwrote:
>#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
Apr 18 '07 #7
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

Apr 19 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

45
by: Matt Parkins | last post by:
Hi, (I realise this probably isn't precisely the right group for this - could someone direct me to the appropriate group to post this question? - thanks !) I'm using Visual C++ 2005 Express...
12
by: Nollie | last post by:
I need to write a couple of my own string manipulation routines (e.g. a strcpy() alternative that returns the number of chars copied). I've started with one of the simpler functions, strlen(). I've...
21
by: sugaray | last post by:
hi, it just came up my mind that since we can get the length of any given string literal S with 'sizeof S-1', so, what's the merit of library function strlen()'s existence ? thanx in advance for...
81
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
33
by: apropo | last post by:
what is wrong with this code? someone told me there is a BAD practice with that strlen in the for loop, but i don't get it exactly. Could anyone explain me in plain english,please? char...
2
by: danielesalatti | last post by:
Hello!! I'm studying c++ and I'm trying to get a little piece of code working, but I'm getting a segfault with strlen here: void tabhash::set (url *U) { uint hash = U->hashCode(); char* url =...
53
by: ¬a\\/b | last post by:
strlen is wrong because can not report if there is some error e.g. char *a; and "a" point to an array of size=size_t max that has no 0 in it
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.