From owner-freebsd-questions@FreeBSD.ORG Wed Oct 24 11:26:03 2007 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 3004D16A417 for ; Wed, 24 Oct 2007 11:26:03 +0000 (UTC) (envelope-from keramida@ceid.upatras.gr) Received: from igloo.linux.gr (igloo.linux.gr [62.1.205.36]) by mx1.freebsd.org (Postfix) with ESMTP id AA09813C4E7 for ; Wed, 24 Oct 2007 11:26:02 +0000 (UTC) (envelope-from keramida@ceid.upatras.gr) Received: from kobe.laptop (dialup226.ach.sch.gr [81.186.70.226]) (authenticated bits=128) by igloo.linux.gr (8.14.1/8.14.1/Debian-9) with ESMTP id l9OBPArg022322 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT) for ; Wed, 24 Oct 2007 14:25:45 +0300 Received: from kobe.laptop (kobe.laptop [127.0.0.1]) by kobe.laptop (8.14.1/8.14.1) with ESMTP id l9OBGcfb002882 for ; Wed, 24 Oct 2007 14:17:08 +0300 (EEST) (envelope-from keramida@ceid.upatras.gr) Received: (from keramida@localhost) by kobe.laptop (8.14.1/8.14.1/Submit) id l9OBFrlZ002874; Wed, 24 Oct 2007 14:15:53 +0300 (EEST) (envelope-from keramida@ceid.upatras.gr) Date: Wed, 24 Oct 2007 14:15:53 +0300 From: Giorgos Keramidas To: Harald Schmalzbauer Message-ID: <20071024111552.GA2765@kobe.laptop> References: <200710232044.53240.h.schmalzbauer@omnisec.de> <20071023220134.3abd635e@epia-2.farid-hajji.net> <20071023162454.93851854.wmoran@potentialtech.com> <200710232324.09851.h.schmalzbauer@omnisec.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <200710232324.09851.h.schmalzbauer@omnisec.de> X-Hellug-MailScanner: Found to be clean X-Hellug-MailScanner-SpamCheck: not spam, SpamAssassin (cached, score=-3.923, required 5, autolearn=not spam, ALL_TRUSTED -1.80, AWL 0.48, BAYES_00 -2.60) X-Hellug-MailScanner-From: keramida@ceid.upatras.gr X-Spam-Status: No Cc: freebsd-questions@freebsd.org Subject: Re: Mentor for C self study wanted X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 24 Oct 2007 11:26:03 -0000 On 2007-10-23 23:24, Harald Schmalzbauer wrote: > Thanks all, > here was my example, just for completeness, I found mentors for my > needs. > #include > > void main() > { > short nnote; > > // Numerischen Notenwert einlesen > printf("Bitte numerischen Schulnotenwert eingeben: "); > scanf("%d",&nnote); You are passing "%d" to scanf() so it expects to find enough 'storage' in its pointer argument for an 'int'. If 'short' happens to have a smaller size (as is commonly the case), scanf() will overwrite random memory locations after 'nnote'. On systems where 'nnote' is stored in the stack (because it's an automatic/local variable of main()), you are risking stack corruption (and a SEGFAULT *may* happen). It's also a very good idea to check the return code of scanf(): int nnote; if (scanf("%d", &nnote) != 1) { error; } > switch (nnote) > { > case 1: printf("Die Note %d entspricht sehr gut.",nnote); > break; > case 2: printf("Die Note %d entspricht gut.",nnote); > break; > case 3: printf("Die Note %d entspricht befriedigend.",nnote); > break; > case 4: printf("Die Note %d entspricht ausreichend.",nnote); > break; > case 5: printf("Die Note %d entspricht mangelhaft.",nnote); > break; > case 6: printf("Die Note %d entspricht ungen?gend.",nnote); > break; > default: printf("%d ist keine zul?ssige Schulnote!"); There's no `int' argument to the printf() call of the default clause. This will either cause printf() to print random garbage, or try to access memory regions which are unmapped and SEGFAULT. > P.S.: > I found that declaring nnote as int soleves my problem, but I couldn?t > understand why. > Another one was the result of default: nnote was -1077942208 instead > of 9 for example. It was never assigned to 9 :)