From owner-freebsd-questions@FreeBSD.ORG Sat May 8 03:31:10 2004 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 6A1D416A4CE for ; Sat, 8 May 2004 03:31:10 -0700 (PDT) Received: from mail.broadpark.no (mail.broadpark.no [217.13.4.2]) by mx1.FreeBSD.org (Postfix) with ESMTP id B489D43D39 for ; Sat, 8 May 2004 03:31:09 -0700 (PDT) (envelope-from henrik.w.lund@broadpark.no) Received: from broadpark.no (52.80-202-129.nextgentel.com [80.202.129.52]) by mail.broadpark.no (Postfix) with ESMTP id 0FFB45A49; Sat, 8 May 2004 12:31:21 +0200 (MEST) Message-ID: <409CAA8C.6090203@broadpark.no> Date: Sat, 08 May 2004 11:38:20 +0200 From: Henrik W Lund User-Agent: Mozilla/5.0 (X11; U; FreeBSD i386; en-US; rv:1.6) Gecko/20040410 X-Accept-Language: no, en-us, en MIME-Version: 1.0 To: Malcolm Kay , freebsd-questions@freebsd.org References: <20040506190033.A45C616A5CF@hub.freebsd.org> <409AAA48.50409@broadpark.no> <200405080948.15164.malcolm.kay@internode.on.net> In-Reply-To: <200405080948.15164.malcolm.kay@internode.on.net> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: segmentation fault-- is my array too long? X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 08 May 2004 10:31:10 -0000 Malcolm Kay wrote: > On Friday 07 May 2004 06:42, Henrik W Lund wrote: > .... > >>malloc() is your friend! :-) >> >>--> double *ncost = malloc(sizeof (double) * persons * scens); <-- >> >>This ought to do the trick. Just remember to make sure that malloc >>returns a valid pointer, otherwise you'll have another seg fault. >> >>I'm pretty sure you can adress the pointer like you do with the array >>there (ncost[persons][0], etc...); if not, you can always do >>ncost(sizeof(double) * persons + 0), etc... >> >>/* AMENDMENT!!! */ >>In my haste, I totally forgot my pointer dereferencing. The correct way >>to reference a pointer as a two dimensional array is, of course, thus: >> >>*(ncost + (sizeof(double) * persons) + 0)) = 0.00; > > > You've still got it wrong! > ncost increments in units of size equal to that which it points > so it should be: > *(ncost + person*scens + scen) > where person is the first index and scen the second. > or in the particular instance > *(ncost + person*scens + 0) = 0.00; > > For easier to read code it would be better to use: > double (*ncost)[scens] = malloc( persons * sizeof *ncost ); > > and dereference as: > ncost[person][scen] > > or in particular > ncost[person][0] = 0.0; > > And for the OP it is usual to write constants generated > with #define in upper-case. It generally seems to help > to make the code easier to follow. In this case: > PERSONS instead of persons > and > SCENS instead of scens > > This also make the distinction between PERSONS and person > more evident while retaining their implied connection. > > Malcolm > > Thank you for the rectification. While the code did compile and run fine the way I wrote it, I suppose that's just lucky. I guess stuff like this is the reason why some of my C/C++ programs seg fault on rare occasions. ;-) I generally handle pointers well, but evidently not perfectlu well. :-D -Henrik W Lund