From owner-freebsd-chat@FreeBSD.ORG Fri Nov 16 06:46:10 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 6559116A418 for ; Fri, 16 Nov 2007 06:46:10 +0000 (UTC) (envelope-from simias.n@gmail.com) Received: from hu-out-0506.google.com (hu-out-0506.google.com [72.14.214.233]) by mx1.freebsd.org (Postfix) with ESMTP id EB9A513C465 for ; Fri, 16 Nov 2007 06:46:08 +0000 (UTC) (envelope-from simias.n@gmail.com) Received: by hu-out-0506.google.com with SMTP id 28so275844hub for ; Thu, 15 Nov 2007 22:46:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:from:to:cc:subject:references:date:in-reply-to:message-id:user-agent:mime-version:content-type; bh=ayRrs1I2zwsD1DQNG2ko+9Y00+OaPA44BXf/34iwbdE=; b=rZXw5J/2/SrfVoLb22ycbkQgR/A6iHbjh/c1dZtozOA2ajxwAVOso0i6JR+dSylhNYF3p7BwteMfD2nkoYyddqh2an/hxtDOJwFzEDPbvsQ+zdIDBCM8zwFt3fqNR04y//7AxS9R4dK+Rf2bRbCBCE5FF/nTM50W3oPXe0VbXfg= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:from:to:cc:subject:references:date:in-reply-to:message-id:user-agent:mime-version:content-type; b=B/bQK5XeVgbI5rgX4NLhdB4Xl7URYKgCfZDJVUvfG9xvQyfFOY43yNJ3MeWwo51/6QmwxvtNewuREuxDy41soXl0ufVSKbp4CVqgboJiHI80mvoyO559U4EhRRl+CYt7UOixu3L6WNlOPlCssLP3ALWIUB52cCUE5Jq1mK3Xng0= Received: by 10.82.177.3 with SMTP id z3mr4253931bue.1195194684366; Thu, 15 Nov 2007 22:31:24 -0800 (PST) Received: from simias.hd.free.fr ( [82.243.51.8]) by mx.google.com with ESMTPS id d23sm3914321nfh.2007.11.15.22.31.22 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 15 Nov 2007 22:31:23 -0800 (PST) From: Simias To: deeptech71@gmail.com References: <473D344C.1080805@gmail.com> Date: Fri, 16 Nov 2007 07:31:06 +0100 In-Reply-To: <473D344C.1080805@gmail.com> (deeptech's message of "Fri, 16 Nov 2007 07:10:20 +0100") Message-ID: <864pfmvh05.fsf@simias.hd.free.fr> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.50 (berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Cc: freebsd-chat@freebsd.org Subject: Re: C out-of-bound pointer question X-BeenThere: freebsd-chat@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list 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 06:46:10 -0000 deeptech71@gmail.com writes: > 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) > _______________________________________________ > freebsd-chat@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-chat > To unsubscribe, send any mail to "freebsd-chat-unsubscribe@freebsd.org" > I don't really understand the problem, *((k-268435410)+26843541x) <=> *(k + x) <=> k[x], so as long as x belongs to [0; 10[ there's no out of bound issue. However, note that in "int* p = k-268435410;", if the address of k is inferior to 268435410, p will old a negative address. This won't be a problem in x86 and most other architectures I'm aware of, but I'm not sure this behaviour is defined by the C standard. (also, I'd use macros, like #define WHATEVER 268435410, it'll make the code way more readable) -- Simias