From owner-freebsd-chat@FreeBSD.ORG Fri Nov 16 07:56:24 2007 Return-Path: Delivered-To: freebsd-chat@FreeBSD.ORG Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 9E7A816A417 for ; Fri, 16 Nov 2007 07:56:24 +0000 (UTC) (envelope-from olli@lurza.secnetix.de) Received: from lurza.secnetix.de (lurza.secnetix.de [IPv6:2001:1b20:1:3::1]) by mx1.freebsd.org (Postfix) with ESMTP id 1133213C45D for ; Fri, 16 Nov 2007 07:56:23 +0000 (UTC) (envelope-from olli@lurza.secnetix.de) Received: from lurza.secnetix.de (localhost [127.0.0.1]) by lurza.secnetix.de (8.14.1/8.14.1) with ESMTP id lAG7uGox017137; Fri, 16 Nov 2007 08:56:22 +0100 (CET) (envelope-from oliver.fromme@secnetix.de) Received: (from olli@localhost) by lurza.secnetix.de (8.14.1/8.14.1/Submit) id lAG7uGA1017136; Fri, 16 Nov 2007 08:56:16 +0100 (CET) (envelope-from olli) Date: Fri, 16 Nov 2007 08:56:16 +0100 (CET) Message-Id: <200711160756.lAG7uGA1017136@lurza.secnetix.de> From: Oliver Fromme To: freebsd-chat@FreeBSD.ORG, deeptech71@gmail.com In-Reply-To: <473D344C.1080805@gmail.com> X-Newsgroups: list.freebsd-chat User-Agent: tin/1.8.3-20070201 ("Scotasay") (UNIX) (FreeBSD/6.2-STABLE-20070808 (i386)) MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-2.1.2 (lurza.secnetix.de [127.0.0.1]); Fri, 16 Nov 2007 08:56:22 +0100 (CET) Cc: Subject: Re: C out-of-bound pointer question X-BeenThere: freebsd-chat@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: freebsd-chat@FreeBSD.ORG, deeptech71@gmail.com List-Id: Non technical items related to the community List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 16 Nov 2007 07:56:24 -0000 deeptech71@gmail.com wrote: > int x[N]; > > Values in x[] are (rand()%10)+268435410, aka 268435410..268435419. > The algorith counts each individual value. > > // v1.0 uses if( x[n] == ___ )'s > > // v2.0: > int k[268435420] = {0}; // k uses almost 1GB of memory > for (n = 0; n < N; n++) { > k[ x[n] ]++; > } > > // v2.1: > int k[10] = {0}; > int* p = k-268435410; > for (n = 0; n < N; n++) { > p[ x[n] ]++; > } > > The values in x[] are guaranteed, so k[ x[n]-268435410 ] are k[0] to > k[9], but is *((k-268435410)+26843541_) safe? (as long as I do no > dereference such out-of-bound memory region) In theory, v2.1 should work if the range of the interval is really guaranteed (you could use assert() to be sure). The C standard allows such index calculations, which -- in the end -- is just pointer arithmetics. However, personally I would use v2.0 because it is easier to read, and it looks less "dangerous", and it might even be a little more efficient. It is true that the k[] array in v2.0 uses 1 GB of mapped memory, *BUT* it does not use 1 GB of RAM. It only uses one page of physical RAM. Remember that RAM is allocated dynamically when it is accessed for the first time, so if you never access k[0..268435409], then no RAM will be allocated for it. Of course, you should make sure that it is a local variable (or a malloc()ed one) that is not initialized, or otherwise the initialization will cause RAM to be allocated. Make sure you initialize only the indices that you need. Best regards Oliver -- Oliver Fromme, secnetix GmbH & Co. KG, Marktplatz 29, 85567 Grafing b. M. Handelsregister: Registergericht Muenchen, HRA 74606, Geschäftsfuehrung: secnetix Verwaltungsgesellsch. mbH, Handelsregister: Registergericht Mün- chen, HRB 125758, Geschäftsführer: Maik Bachmann, Olaf Erb, Ralf Gebhart FreeBSD-Dienstleistungen, -Produkte und mehr: http://www.secnetix.de/bsd "With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead." -- RFC 1925