From owner-freebsd-questions@freebsd.org Thu Apr 6 00:58:55 2017 Return-Path: Delivered-To: freebsd-questions@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id B398BD31579; Thu, 6 Apr 2017 00:58:55 +0000 (UTC) (envelope-from kaduk@mit.edu) Received: from dmz-mailsec-scanner-6.mit.edu (dmz-mailsec-scanner-6.mit.edu [18.7.68.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 4F62B14C; Thu, 6 Apr 2017 00:58:54 +0000 (UTC) (envelope-from kaduk@mit.edu) X-AuditID: 12074423-9c3ff700000003ba-87-58e591983499 Received: from mailhub-auth-1.mit.edu ( [18.9.21.35]) (using TLS with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by (Symantec Messaging Gateway) with SMTP id 51.B5.00954.89195E85; Wed, 5 Apr 2017 20:53:45 -0400 (EDT) Received: from outgoing.mit.edu (outgoing-auth-1.mit.edu [18.9.28.11]) by mailhub-auth-1.mit.edu (8.13.8/8.9.2) with ESMTP id v360rh1U031895; Wed, 5 Apr 2017 20:53:44 -0400 Received: from kduck.kaduk.org (24-107-191-124.dhcp.stls.mo.charter.com [24.107.191.124]) (authenticated bits=56) (User authenticated as kaduk@ATHENA.MIT.EDU) by outgoing.mit.edu (8.13.8/8.12.4) with ESMTP id v360rc4Z005338 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT); Wed, 5 Apr 2017 20:53:42 -0400 Date: Wed, 5 Apr 2017 19:53:38 -0500 From: Benjamin Kaduk To: Mario Lobo Cc: freebsd-questions@freebsd.org, FreeBSD Hackers Subject: Re: [OFF-TOPIC] C question Message-ID: <20170406005338.GR30306@kduck.kaduk.org> References: <20170405132251.536ab064@Papi> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20170405132251.536ab064@Papi> User-Agent: Mutt/1.6.1 (2016-04-27) X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrHIsWRmVeSWpSXmKPExsUixCmqrDtz4tMIg687ZC22b/7HaPHy6yYW i8/rVzA6MHtsnf6H1WPGp/ksAUxRXDYpqTmZZalF+nYJXBnLP6xhLGjjrdjVHNnAuJSri5GT Q0LAROLD6UPsXYxcHEICbUwSx3e8YIVwNjBKXPnxiwXCucIkcX7jZiaQFhYBFYnNfUeYQWw2 ILuh+zKYLSKgIDHp1QY2EJtZIFJi1uHVYPXCAsoSrz43AtkcHLxA6+6ezQcJCwloSZy8dgis lVdAUOLkzCcsEK1aEjf+vQQrZxaQllj+jwMkzCmgLdG66yEjiC0KNLFhxgPmCYwCs5B0z0LS PQuhewEj8ypG2ZTcKt3cxMyc4tRk3eLkxLy81CJdM73czBK91JTSTYygYGV3Ud7B+LLP+xCj AAejEg+vx+MnEUKsiWXFlbmHGCU5mJREeRV8gEJ8SfkplRmJxRnxRaU5qcWHGCU4mJVEeF+0 P40Q4k1JrKxKLcqHSUlzsCiJ84prNEYICaQnlqRmp6YWpBbBZGU4OJQkeAMmADUKFqWmp1ak ZeaUIKSZODhBhvMADe8CqeEtLkjMLc5Mh8ifYlSUEue92g+UEABJZJTmwfWCkolE9v6aV4zi QK8I8x4CaecBJiK47ldAg5mABj+58xBkcEkiQkqqgVH2+5yMO+Ehmvz9Vydk9lTulpGd7sV9 +aGXrElc324hm+5b66dpitmyZauYZczX/s9yRGjC509Z18REeQQqeBeUSV/t+XOunpdta3eo iEX/nD+HtmhXa6oqSkz8YRKxtco2sfRG7uelZ248mGb/fM0Mq90dlY+CZbaX5DQt1tLZxNR9 dbcgrxJLcUaioRZzUXEiAD5vuYkBAwAA X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 06 Apr 2017 00:58:55 -0000 On Wed, Apr 05, 2017 at 01:23:16PM -0300, Mario Lobo wrote: > Hi There ! > > I don't know if this list is appropriate for this. > > if it isn't, please point me to the right direction. It would probably be more appropriate on something like StackOverflow. > > How can I dynamically change the level of indirection? > > > How can I dynamically switch: > > ---------------- > KFNODE ***Nodes; to KFNODE ****Nodes; > ---------------- > Nodes = (KFNODE ***) malloc(5 * sizeof(KFNODE **)); > to Nodes = (KFNODE ****) malloc(5 * sizeof(KFNODE ***)); > ---------------- > > and so forth? > > Is this possible at all? It is a rather unnatural thing to want to do, so the solution would also be rather unnatural. It seems, though, that you could have a loop construct that uses void* and void** for all levels except for the leaf, since the underlying property of the non-leaf allocations is that they are to hold pointers. As you prepare to go to the next layer of indirection you cast the pointers from void* to void** and dereference them. Once your depth gets to the last level then you can cast he void* to KFNODE* and actually handle the leaf elements. But as I said, this is rather unusual style to do, and would require replacing the x[a][b][c] with a loop that dereferences successive pointers. -Ben P.S. All those extra allocations for intermediate arrays add up to a fair bit of storage, and the extra cost of all the pointer indirections does, too. Sometimes it's more efficent to just allocate a "flat" array of KFDNODE[x*y*z] and manually do index arithmetic to emulate a multi-dimensional array.