#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 = "abcdefghijklmn opqrstuvwxyz";
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. 7 2886
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
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 = "abcdefghijklmn opqrstuvwxyz";
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.
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.
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.
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 = "abcdefghijklmn opqrstuvwxyz";
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
On 18 Apr 2007 09:59:27 -0700, in comp.lang.c , Duke
<xa****@gmail.c omwrote:
>#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 = "abcdefghijklmn opqrstuvwxyz";
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
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 = "abcdefghijklmn opqrstuvwxyz";
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.securityfoc us.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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 Edition Beta (free download from MS -
hooray!), and everything works fine, except I get warnings back on the use
of some functions, strlen() for example, saying that the function has been
deprecated - although they do still work (which is I guess...
|
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 written a
very simple version call StringLength(), but it performs significantly
slower than its CRT counterparts.
Here's my implementation:
inline unsigned int StringLength( const char *pszString )
{
unsigned int cch = 0;
|
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 your
instruction.
|
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 be any advantage in having strcat and strcpy return a
pointer to the "end" of the destination string rather than returning a
|
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 *reverse(char *s)
{
int i;
char *r;
if(!s) return NULL;//ERROR
r=calloc(strlen(s)+1,sizeof(char));
| |
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 = U->giveUrl();
char* chash = (char*)hash;
char* Insert1="INSERT INTO sep_hashtable VALUES (";
|
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
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
| |
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |