473,468 Members | 1,384 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Why no segmentation fault

Hi,

I'm wondering why this program does not crash with a segmentation
fault:

#include <malloc.h>
#include <string.h>
#include <stdio.h>

int main()
{
char *array;

array = (char*)malloc(10 * sizeof(char) );
if ( array == NULL )
exit( 0 );

strcpy( array, "11223456789\0");

printf( "\narray[11]: %c\n", array[11] )

return 0;
}

I allocate space for 10 characters on the system heap and then copy
a string of size 12 into the allocated space. Why does the program not
crash?
My understanding was so far:
malloc request some new free space on the system heap. So, first the
program memory manager is consulted to check if it is already assigned
some free space by the operating system that might be suitable for the
malloc request. If so, this memory segment is used. Otherwise, the
request is directed to the OS that provides some new free memory that
is now assigned to this program (process) and used for the malloc memory
allocation. However, when I call "strcpy( array, "11223456789\0")" I
write the first 10 characters to the allocated memory area.The
remaining 2 characters exceed the memory area I was granted access to
and are tried to be written to memory I have no write access to. That
illegal memory access should be noticed by the OS that terminates the
program with a segmentation fault.

Furthermore, my printf should also crash the program since I
illegally attempt to read from memory I have no access to.

Thank you.

Chris

Mar 26 '06
59 2830
Mark McIntyre <ma**********@spamcop.net> writes:
On Tue, 28 Mar 2006 20:34:33 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.org> wrote:
It's true that an implementation *may* define EXIT_SUCCESS as
something other than 0, but there's really no reason to do so.


Sure. On the other hand, there's no reason for EXIT_SUCCESS to be
defined as zero either, since it isn't obligated to be zero.


Sure there is.

Defining EXIT_SUCCESS as something other than 0 means that,
potentially, a program using exit(0) could behave differently than a
program using exit(EXIT_SUCCESS). From a programmer's point of view,
the choice of which one to use normally is (and should be) completely
arbitrary.

If an implementation wants to define multiple successful status codes,
it can define its own; distinguishing between EXIT_SUCCESS and 0 is
just obfuscation.

--
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.
Mar 29 '06 #51
On Wed, 29 Mar 2006 10:17:21 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.org> wrote:
Mark McIntyre <ma**********@spamcop.net> writes:
On Tue, 28 Mar 2006 20:34:33 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.org> wrote:
It's true that an implementation *may* define EXIT_SUCCESS as
something other than 0, but there's really no reason to do so.
Sure. On the other hand, there's no reason for EXIT_SUCCESS to be
defined as zero either, since it isn't obligated to be zero.


Sure there is.


*shrug*. Oh, no he isn't, oh yes he is. Look out behind you.
Defining EXIT_SUCCESS as something other than 0 means that,
potentially, a program using exit(0) could behave differently than a
program using exit(EXIT_SUCCESS).
No. According to the ISO Standard, they /must/ both mean "successful
termination status".
From a programmer's point of view,
the choice of which one to use normally is (and should be) completely
arbitrary.


PMF but from the point of view of a C programme, you're talking
bollocks. A C programme knows nothing and cares nothing for what the
OS does after termination. It returns zero or EXIT_SUCCESS, both
indicate it succeeded, if the OS chooses to intrepret 0 and 34567 as
both meaning "successful termination", then fine. Why does the
programmer care?
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
Mar 29 '06 #52
On 2006-03-29, Mark McIntyre <ma**********@spamcop.net> wrote:
On Wed, 29 Mar 2006 10:17:21 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.org> wrote:
Mark McIntyre <ma**********@spamcop.net> writes:
On Tue, 28 Mar 2006 20:34:33 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.org> wrote:

It's true that an implementation *may* define EXIT_SUCCESS as
something other than 0, but there's really no reason to do so.

Sure. On the other hand, there's no reason for EXIT_SUCCESS to be
defined as zero either, since it isn't obligated to be zero.


Sure there is.


*shrug*. Oh, no he isn't, oh yes he is. Look out behind you.
Defining EXIT_SUCCESS as something other than 0 means that,
potentially, a program using exit(0) could behave differently than a
program using exit(EXIT_SUCCESS).


No. According to the ISO Standard, they /must/ both mean "successful
termination status".


But they can mean DIFFERENT successful termination statuses.
Mar 29 '06 #53
On 2006-03-29, Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
Jordan Abel <ra*******@gmail.com> wrote:
On 2006-03-27, Mark McIntyre <ma**********@spamcop.net> wrote:
> I strongly suspect VMS lets you have a forest of possible different
> "it worked" results.
> 0 = VMS-I-SUCCESS successful programme termination
> 64 = VMS-I-SUCCESS successful cluster shutdown
> 128 = VMS-I-SUCCESS successfully ejected the warp core


But why use a different one for 0 than for EXIT_SUCCESS?


Because on OSes where the difference can be significant, it is nice to
be able to return both "program terminated successfully by default" and
"program terminated successfully by explicit decision of the
programmer"?


And which one do you suppose would "0" map to, and which EXIT_SUCCESS?
Given that an explicit decision of the programmer is equally likely to
be either, it might make more sense for exit() to do bizarre
platform-specific stuff to figure out where it was called from.
Mar 29 '06 #54
On 29 Mar 2006 15:36:48 GMT, in comp.lang.c , Jordan Abel
<ra*******@gmail.com> wrote:
On 2006-03-29, Mark McIntyre <ma**********@spamcop.net> wrote:
On Wed, 29 Mar 2006 10:17:21 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.org> wrote:
Defining EXIT_SUCCESS as something other than 0 means that,
potentially, a program using exit(0) could behave differently than a
program using exit(EXIT_SUCCESS).


No. According to the ISO Standard, they /must/ both mean "successful
termination status".


But they can mean DIFFERENT successful termination statuses.


So? The standard doesn't care what happens once the OS gets the
message.
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
Mar 29 '06 #55
Mark McIntyre <ma**********@spamcop.net> writes:
On Wed, 29 Mar 2006 10:17:21 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.org> wrote:

[...]
Defining EXIT_SUCCESS as something other than 0 means that,
potentially, a program using exit(0) could behave differently than a
program using exit(EXIT_SUCCESS).


No. According to the ISO Standard, they /must/ both mean "successful
termination status".

[...]

But possibly different forms of successful termination status.

C99 7.20.4.3p5:

If the value of status is zero or EXIT_SUCCESS, an
implementation-defined form of the status _successful termination_
is returned.

The wording is ambiguous. It could mean either:

Zero causes an implementation-defined form of successful
termination.

EXIT_SUCCESS causes an implementation-defined form of successful
termination.

(They needn't be the same form, as long as they both indicate
success.)

or:

Zero or EXIT_SUCCESS causes *the same* implementation-defined form
of successful termination.

I think the first interpretation is more reasonable (YMMV). Under
that interpretation, exit(0) and exit(EXIT_SUCCESS) could behave
differently, by causing two different forms of a successful
termination status to be returned to the host environment.

I'm arguing that an implementation is allowed to make exit(0) and
exit(EXIT_SUCCESS) behave differently (though the difference can't be
detected by the program), not that there's any point in doing so.

--
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.
Mar 29 '06 #56
Mark McIntyre <ma**********@spamcop.net> writes:
On 29 Mar 2006 15:36:48 GMT, in comp.lang.c , Jordan Abel
<ra*******@gmail.com> wrote:
On 2006-03-29, Mark McIntyre <ma**********@spamcop.net> wrote:
On Wed, 29 Mar 2006 10:17:21 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.org> wrote:
Defining EXIT_SUCCESS as something other than 0 means that,
potentially, a program using exit(0) could behave differently than a
program using exit(EXIT_SUCCESS).

No. According to the ISO Standard, they /must/ both mean "successful
termination status".


But they can mean DIFFERENT successful termination statuses.


So? The standard doesn't care what happens once the OS gets the
message.


All I meant when I said they could "behave differently" is that they
could return different statuses to the environment. Obviously the
program itself can't tell the difference (but another program,
invoking our program via system(), could).

--
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.
Mar 29 '06 #57
On Wed, 29 Mar 2006 20:54:16 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.org> wrote:
All I meant when I said they could "behave differently" is that they
could return different statuses to the environment.


Then I think we're agreeing violently with each other... :-)
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
Mar 29 '06 #58
In article <e0**********@news1brm.Central.Sun.COM>,
Eric Sosman <Er*********@sun.com> wrote:
That's why I think the people who ask "Which
language is best?" are misguided (and those who
claim "Language X Rulez!" or "Language Y Sucks!"
are even more so).
Butbutbut... Language X *does* "Rulez", and Language Y *does* suck.
It's just that what languages Language X and Language Y are depends on
the task at hand.

F'rexample:

Task: Processing text
Language X: sh+awk+sed, Perl
Language Y: Fortran

Task: Producing write-only code
Language X: APL, Perl
Language Y: COBOL

Task: Making peoples' brains explode
Language X: Forth, Lisp
Language Y: Pascal
ObTopic:
Task: Interfacing with OS (or other) API libraries
Task: Writing code that will compile and run anywhere you can imagine
and a few places you can't
Language X: C

Task: Quickly building a simple GUI program
Task: Teaching good programming habits directly[1]
Language Y: C
dave

[1] Note that C can be pretty good for "Shoot yourself in the foot enough
times in enough different ways and you'll learn how to avoid doing
it", which is sometimes a useful complement to teaching directly.

--
Dave Vandervies dj******@csclub.uwaterloo.ca That fluorescent colour is surely not found in nature.

You need to go outside more often.
--Prabhakar Ragde and Doug Payne in uw.general
Mar 31 '06 #59
Jordan Abel <ra*******@gmail.com> wrote:
On 2006-03-29, Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
Jordan Abel <ra*******@gmail.com> wrote:
On 2006-03-27, Mark McIntyre <ma**********@spamcop.net> wrote:
> I strongly suspect VMS lets you have a forest of possible different
> "it worked" results.
> 0 = VMS-I-SUCCESS successful programme termination
> 64 = VMS-I-SUCCESS successful cluster shutdown
> 128 = VMS-I-SUCCESS successfully ejected the warp core

But why use a different one for 0 than for EXIT_SUCCESS?
Because on OSes where the difference can be significant, it is nice to
be able to return both "program terminated successfully by default" and
"program terminated successfully by explicit decision of the
programmer"?


And which one do you suppose would "0" map to, and which EXIT_SUCCESS?


0 the unspecific one, and EXIT_SUCCESS the specific.
Given that an explicit decision of the programmer is equally likely to
be either,


Possibly; but the other way 'round, EXIT_SUCCESS is more likely to be an
explicit decision than 0. One sees return 0; in lots of sample and
teaching code. EXIT_SUCCESS is rarer.

Richard
Mar 31 '06 #60

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

Similar topics

2
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...
3
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:...
16
by: laberth | last post by:
I've got a segmentation fault on a calloc and I don'tunderstand why? Here is what I use : typedef struct noeud { int val; struct noeud *fgauche; struct noeud *fdroit; } *arbre; //for those...
3
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...
5
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...
18
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)?...
27
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! ...
7
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...
3
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...
6
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
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...
0
agi2029
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,...
1
isladogs
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.