Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!ukma!nrl-cmf!cmcl2!brl-adm!adm!I1090801%DBSTU1.BITNET@CUNYVM.CUNY.EDU From: I1090801%DBSTU1.BITNET@CUNYVM.CUNY.EDU Newsgroups: comp.lang.c Subject: Check for function results Message-ID: <10530@brl-adm.ARPA> Date: Tue, 24-Nov-87 23:49:14 EST Article-I.D.: brl-adm.10530 Posted: Tue Nov 24 23:49:14 1987 Date-Received: Sat, 28-Nov-87 07:49:20 EST Sender: news@brl-adm.ARPA Lines: 35 I am a student of computer science and I have to write a C-compiler. The problem I came across is: does a C-compiler have to generate code to check if a function really delivers a result in all cases? Example: In the following (nonsense) function int f() { int a; a = 1; /* only to set a to a non-garbage value */ if (...) return a; else ... /* statements without any return */ } a value is delivered in the then part but in the else part nothing happens and the program just runs out of the end of the function. So, in this case, some garbage is delivered. In this example you can easily see this, but things get worse if the function is more complicated. I think it isn't easy for a compiler to see all this at compile-time, so things should be checked during runtime of a program. My idea is to generate code that sets an internal flag to FALSE at the start of the function. Each return(expression) should set this flag to TRUE and then jump to the end of the function where the return is handled. Here the flag should be checked and an error- message be generated if it is still FALSE. This way it can be deteced if the way through the function passed a return(expression). I wonder if all this is really necessary, but I can't find anything about this problem in K&R. Do other compilers check this? I think, some strange errors can be detected by this method. Maybe someone on the net can help me. Ulf Gruene I1090801@DBSTU1.BITNET Technische Universitaet Braunschweig West Germany