Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

murrpurr

Name: Anonymous 2011-02-22 8:49


/*
 * Copyright (C) 1904-2011 by Anon Y. Mous.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation under the terms of the GNU General Public License is hereby
 * granted. No representations are made about the suitability of this software
 * for any purpose. It is provided "as is" without express or implied warranty.
 * See the GNU General Public License for more details.
 *
 * Documents produced by Doxygen are derivative works derived from the
 * input used in their production; they are not affected by this license.
*/
#include <stdio.h>

int main()
{
    /* this is a highly complex algorithm and you are
       not expected to understand it */
    fprintf(stderr, "Segmentation fault\n");
    return 139;
}

Name: Anonymous 2011-02-22 16:38

>>6
Because printing "Segmentation fault" to stderr and returning 139 is the default behaviour on some *nix'es. In other OSes, the behaviour will vary. For example, in Windows, it'll invoke the SEH, then VEH (or other handlers), if those can't handle the access violation error, it will call UnhandledExceptionFilter, which can also be set by the user, but if it's not, it will then display an error or not do anything (depending on settings), or if you have a JIT debugger set, it will launch the debugger neatly allowing you to see what caused the application crash. Other OSes have different ways of handling it as well.

The point is that your program tries to create the illusion that a segmentation fault happened, and this illusion would only work in OSes which actually display such errors and return with that error code. Since this will only produce the required effect in some OSes, it's not portable in the truest sense as while the code is portable, the behavior is not the expected one. Instead, you should generate a real segmentation fault which will give consistent behavior across a wider range of OSes. The problem with generating a real segmentation fault is that you need to know which area of memory is protected (if you're even in an OS which has such a thing, for example you can't cause a segmentation fault/access violation in an application running in x86 real mode (such as DOS), or in various microcontrollers).

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List