-> Is there any standard for when a sementation fault can occur; I mean should it occur if we access 0, or code segment or anything else ?
-> Why a system crashes, when we are trying to just "read" a memory area which is protected ?
(I can understand that system should crash if we try to "write" something.)
-> How the system knows that, we are accessing a prohibited area; will it check that before accessing any pointer ? If that is so, won't it be time consuming ?
8 2654 donbock 2,426
Recognized Expert Top Contributor
The unary * operator denotes indirection. ... If an invalid value has been assigned to the pointer, the behavior of the unary * operator is undefined. (83)
83) ... Among the invalid values for dereferencing a pointer by the unary * operator are a null pointer, an address inappropriately aligned for the type of object pointed to, and the address of an object after the end of its lifetime.
Segmentation fault is not mentioned in the Standard. The Standard allows anything to happen if you dereference an invalid pointer -- including segmentation fault. Notice the Standard does not require undefined behavior to be consistent -- some invalid pointers may cause segmentation faults, while others may not.
The Standard says that both read and write through an invalid pointer are undefined behavior.
Not all systems provide segmentation faults. Typically, segmentation fault is only supported by systems where it is easy to detect use of invalid pointers; for example target systems with a memory management unit (MMU).
Why should anybody care if you merely read through an invalid pointer? Some processors use an asynchronous bus architecture -- memory access timing is nondeterministic, the access completes when the memory device announces it is finished. What if you access nonexistent memory? There is no memory device to announce completion of the access. Rather than hang forever, these processors have a failsafe timer that aborts bus transactions that take too long. Triggering the failsafe timeout (called 'bus error') is a pretty serious exception. Another reason not to go off and read arbitrary memory is memory-mapped I/O. Some I/O devices change state when you read them; for example, reading the receive-register of a UART causes the received character to be discarded.
Thanks Donbock. Can you also clarify my third question that, how does a system know if to cause a "seg fault" when prohibited memory is accessed ?
for example, following code caused seg. fault:
int *p = 0x<CODE_SEGMENT>;
int i = *p; // seg. fault
Will system always check if the 'p' is between the protected areas of code segment. And if this is the case, aren't we compromising the execution time at a large extent ?
Banfa 9,065
Recognized Expert Moderator Expert
Generally segmentation faults come from software or hardware subsystems that have to perform address translation of some sort. When handed an invalid address they can't do it and raise an exception in hardware that is ultimately translated into a seg fault in software.
Platforms that don't have this sort of address translator generally don't produce seg faults, just very strange behaviour as you read and write memory that you shouldn't be.
donbock 2,426
Recognized Expert Top Contributor
The C Standard neither requires nor forbids an implementation to embed the type of run-time checking that you propose. As a practical matter, I don't think you will find any compilers that spontaneously inject run-time pointer-validation code. As Banfa pointed out, all run-time ponter checking we're aware of is performed by hardware.
Frinavale 9,735
Recognized Expert Moderator Expert
I'm curious.
I'm I correct in saying that it is the Operating System that determines when your application is attempting to access memory outside of what has been allocated for use by the application?
It is the OS that creates the core dump files...right?
-Frinny
I believe so. Once version of Windows checked the value of the CR12 register after the translation from the logical to the physical disk address. If that register changed, then the address was outsode the process address space and triggered an access violation.
Banfa 9,065
Recognized Expert Moderator Expert
Not necessarily, for instance I have worked with plenty of embedded applications that had no operating system as such, apart from the fairly basic multi-threading one used such ThreadX. However they did have a hardware memory management units.
As far as the processor and operating system was concerned any address is valid but the memory management unit which is a hardware component within the processor is aware of what logical addresses map to actual physical addresses and raises an actual hardware interrupt on the processor when it tries to access and address that maps nowhere.
My point here Frinny is that you are making a common oversight of assuming that when talking C/C++ the platform is a PC of some sort but actually that is an extremely limit view of the possible platforms that run code compiled from C/C++. I suspect that C/C++ is used in writing code for a wider range of platforms than almost any other language from 8 bit microprocessors limited to a few kbytes of internal memory up through PCs and beyond.
Trying to generalise about the actions of such a wide variety of operating systems is at best unlikely to cover the full range of possibilities.
Frinavale 9,735
Recognized Expert Moderator Expert Sign in to post your reply or Sign up for a free account.
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: 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...
|
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: 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: 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,...
| |
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: 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: 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,...
|
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: 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 ...
|
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...
| |