Hi everyone, these days I find a problem.Look this source code:
1 #include <stdio.h>
2
3 int
4 main(void)
5 {
6 char a[] = "hello";
7 a[0] = 'X';
8 printf("%s\n", a);
9 char *p = "world";
10 p[0] = 'Y';
11 printf("%s\n", p);
12 exit(0);
13 }
It coule compile correctly.But if i run it. it will give me a
Segmentation fault.
More oddly, if i use GDB to dubeg it, i run it step by step, it will
work well, the GDB display: Program exited normally.
Can anyone tell me why does it happen? 7 1857
On Wednesday 15 March 2006 08:04, Kies opined (in
<11*********************@e56g2000cwe.googlegroups. com>): Hi everyone, these days I find a problem.Look this source code: 1 #include <stdio.h> 2 3 int 4 main(void) 5 { 6 char a[] = "hello";
This initialises `char` array `a` to {'h', ..., 'o', '\0'}.
7 a[0] = 'X';
It's perfectly legal to modify non-constant array elements.
8 printf("%s\n", a); 9 char *p = "world";
This assigns the address of a constant string literal "world" to a
`char` pointer `p`.
10 p[0] = 'Y';
It is not allowed to modify constants.
11 printf("%s\n", p); 12 exit(0); 13 } It coule compile correctly.But if i run it. it will give me a Segmentation fault. More oddly, if i use GDB to dubeg it, i run it step by step, it will work well, the GDB display: Program exited normally. Can anyone tell me why does it happen?
You invoke Undefined Behaviour -- anything can happen.
Look at the <c-faq.com> 8.5 for guidance.
--
BR, Vladimir
Luck can't last a lifetime, unless you die young.
-- Russell Banks
On 2006-03-15, Kies <ll****@gmail.com> wrote: Hi everyone, these days I find a problem.Look this source code: 1 #include <stdio.h> 2 3 int 4 main(void) 5 { 6 char a[] = "hello"; 7 a[0] = 'X'; 8 printf("%s\n", a); 9 char *p = "world"; 10 p[0] = 'Y';
You shouldn't modify string constants. It's better therefore to write:
const char *p = "world";
That way it won't compile, and you know about it sooner.
a[], on the other hand, is a local array, initialized with a string
constant. It's fine to modify a[].
More oddly, if i use GDB to dubeg it, i run it step by step, it will work well, the GDB display: Program exited normally. Can anyone tell me why does it happen?
No idea. gdb will normally trap SIGSEGV.
"Vladimir S. Oka" <no****@btopenworld.com> writes: On Wednesday 15 March 2006 08:04, Kies opined (in <11*********************@e56g2000cwe.googlegroups. com>):
[...] 9 char *p = "world";
This assigns the address of a constant string literal "world" to a `char` pointer `p`.
10 p[0] = 'Y';
It is not allowed to modify constants.
More precisely, it's not allowed (i.e., invokes undefined behavior) to
modify string literals. (The term "constants" could mean any of
several things.)
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Kies wrote: Hi everyone, these days I find a problem.Look this source code: 1 #include <stdio.h> 2 3 int 4 main(void) 5 { 6 char a[] = "hello"; 7 a[0] = 'X'; 8 printf("%s\n", a); 9 char *p = "world"; 10 p[0] = 'Y';
Since p points to string literal. According to c standard modifying its
value can lead to undefined behavior.
compilers put these literals in read only memory. so if you modify the
value you get segmentaion fault at run time.
11 printf("%s\n", p); 12 exit(0); 13 } It coule compile correctly.But if i run it. it will give me a Segmentation fault. More oddly, if i use GDB to dubeg it, i run it step by step, it will work well, the GDB display: Program exited normally. Can anyone tell me why does it happen?
ranjmis wrote: Kies wrote:
Hi everyone, these days I find a problem.Look this source code: 1 #include <stdio.h> 2 3 int 4 main(void) 5 { 6 char a[] = "hello"; 7 a[0] = 'X'; 8 printf("%s\n", a); 9 char *p = "world"; 10 p[0] = 'Y'; Since p points to string literal. According to c standard modifying its value can lead to undefined behavior.
Not "can" -- "does".
compilers put these literals in read only memory. so if you modify the value you get segmentaion fault at run time.
Not necessarily. That's the beauty of undefined behaviour -- anything
can happen.
--
BR, Vladimir
On Wed, 15 Mar 2006 08:14:54 +0000 (UTC), "Vladimir S. Oka"
<no****@btopenworld.com> wrote in comp.lang.c: On Wednesday 15 March 2006 08:04, Kies opined (in <11*********************@e56g2000cwe.googlegroups. com>):
Hi everyone, these days I find a problem.Look this source code: 1 #include <stdio.h> 2 3 int 4 main(void) 5 { 6 char a[] = "hello";
This initialises `char` array `a` to {'h', ..., 'o', '\0'}.
7 a[0] = 'X';
It's perfectly legal to modify non-constant array elements.
8 printf("%s\n", a); 9 char *p = "world";
This assigns the address of a constant string literal "world" to a `char` pointer `p`.
While your overall description is correct, your terminology is quite
wrong.
The OP's line 9 causes the creation of an array of 6 characters
containing the string literal. Like all string literals in C, this
array has the type "array of 6 char", and NOT "array of 6 const char".
The type of a string literal in C is always "array of char", and never
"array of const char". 10 p[0] = 'Y';
It is not allowed to modify constants.
Actually, "allowed" is not particularly good wording here. Of course
it is "allowed", it is only that the result is undefined.
But that doesn't apply here, whatever word you use, because p[0] is a
modifiable char, not a const char.
Attempting to modify a string literal produces undefined behavior in C
because the C standard specifically states that face, not because the
array holding the string literal is composed of const chars. 11 printf("%s\n", p); 12 exit(0); 13 } It coule compile correctly.But if i run it. it will give me a Segmentation fault. More oddly, if i use GDB to dubeg it, i run it step by step, it will work well, the GDB display: Program exited normally. Can anyone tell me why does it happen?
You invoke Undefined Behaviour -- anything can happen.
Look at the <c-faq.com> 8.5 for guidance.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
On Thursday 16 March 2006 03:15, Jack Klein opined (in
<ll********************************@4ax.com>): On Wed, 15 Mar 2006 08:14:54 +0000 (UTC), "Vladimir S. Oka" <no****@btopenworld.com> wrote in comp.lang.c:
On Wednesday 15 March 2006 08:04, Kies opined (in <11*********************@e56g2000cwe.googlegroups. com>):
> Hi everyone, these days I find a problem.Look this source code: > 1 #include <stdio.h> > 2 > 3 int > 4 main(void) > 5 { > 6 char a[] = "hello"; This initialises `char` array `a` to {'h', ..., 'o', '\0'}.
> 7 a[0] = 'X';
It's perfectly legal to modify non-constant array elements.
> 8 printf("%s\n", a); > 9 char *p = "world";
This assigns the address of a constant string literal "world" to a `char` pointer `p`.
While your overall description is correct, your terminology is quite wrong.
I agree, from the pedantic point of view. From the practical point of
view, I'd prefer "loose" -- and IMHO giving the OP just the guidance he
needs.
The OP's line 9 causes the creation of an array of 6 characters containing the string literal. Like all string literals in C, this array has the type "array of 6 char", and NOT "array of 6 const char".
I never said "array of 6 const char". Rather loosely, I said "constant
string literal", "constant" being the loose one. I realise just "string
literal" may be more precise, but I wanted to make a point that these
cannot/should not/are not allowed to be modified. I felt this was more
important to the OP.
The type of a string literal in C is always "array of char", and never "array of const char".
> 10 p[0] = 'Y'; It is not allowed to modify constants.
Actually, "allowed" is not particularly good wording here. Of course it is "allowed", it is only that the result is undefined.
Remember that here we're talking to someone who's most likely a novice
programmer. Is it more instructive to tell them "this is undefined
behaviour -- demons fly out of your nose" or "don't do that -- it's not
allowed"? Especially since the explanation of UB must include the
reference to "it is even allowed to work on some implementations, some
of the time", and FWIW, in my experience, novices tend to take that as
a license to experiment, find out that it actually works reliably
(every single one out of ten times they tried) on their tool chain,
exclaim "Cool! On this system it works *faster*!", and leave it for the
maintainer to suffer the consequences.
But that doesn't apply here, whatever word you use, because p[0] is a modifiable char, not a const char.
Only the one that makes DS9K nuke Mars when written to? Say "modifiable"
and someone is going to do it.
Attempting to modify a string literal produces undefined behavior in C because the C standard specifically states that face, not because the array holding the string literal is composed of const chars.
Again "const char" was never mentioned in my post. For UB, see above.
I guess I should be ritually presented with pemo's "not a pedant" sig,
now that Keith wants to strip it off him. ;-)
--
BR, Vladimir
Calvin: "I wonder where we go when we die."
Hobbes: "Pittsburgh?"
Calvin: "You mean if we're good or if we're bad?" This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: sivignon |
last post by:
Hi,
I'm writing a php script which deals with 3 ORACLE databases.
This script is launch by a script shell on an linux machine like this :
/../php/bin/php ./MySript.php (PHP 4.3.3)
My script...
|
by: diyanat |
last post by:
i am writing a cgi script in C using the CGIC library, the script
fails to run, i am using apache on linux
error report from apache : internal server error
Premature end of script headers:...
|
by: Zheng Da |
last post by:
Program received signal SIGSEGV, Segmentation fault.
0x40093343 in _int_malloc () from /lib/tls/libc.so.6
(gdb) bt
#0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6
#1 0x40094c54 in malloc...
|
by: Fra-it |
last post by:
Hi everybody,
I'm trying to make the following code running properly, but I can't get
rid of the "SEGMENTATION FAULT" error message when executing.
Reading some messages posted earlier, I...
|
by: Digital Puer |
last post by:
Hi, I'm coming over from Java to C++, so please bear with me.
In C++, is there a way for me to use exceptions to catch
segmentation faults (e.g. when I access a location off
the end of an array)?...
| |
by: Paminu |
last post by:
I have a wierd problem.
In my main function I print "test" as the first thing. But if I run the call
to node_alloc AFTER the printf call I get a segmentation fault and test is
not printed!
...
|
by: James Dow Allen |
last post by:
The gcc compiler treats malloc() specially! I have no
particular question, but it might be fun to hear from
anyone who knows about gcc's special behavior.
Some may find this post interesting;...
|
by: pycraze |
last post by:
I would like to ask a question. How do one handle the exception due to
Segmentation fault due to Python ? Our bit operations and arithmetic
manipulations are written in C and to some of our...
|
by: madunix |
last post by:
My Server is suffering bad lag (High Utlization)
I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2
Web visitors retrieve data from the web by php calls through oci
cobnnection...
|
by: DanielJohnson |
last post by:
int main()
{
printf("\n Hello World");
main;
return 0;
}
This program terminate just after one loop while the second program
goes on infinitely untill segmentation fault (core dumped) on...
|
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...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
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...
| |
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 ...
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |