From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752611AbdCEPot convert rfc822-to-8bit (ORCPT ); Sun, 5 Mar 2017 10:44:49 -0500 Received: from unicorn.mansr.com ([81.2.72.234]:34972 "EHLO unicorn.mansr.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752290AbdCEPos (ORCPT ); Sun, 5 Mar 2017 10:44:48 -0500 X-Greylist: delayed 4202 seconds by postgrey-1.27 at vger.kernel.org; Sun, 05 Mar 2017 10:44:48 EST From: =?iso-8859-1?Q?M=E5ns_Rullg=E5rd?= To: Tomas Winkler Cc: "linux-kernel\@vger.kernel.org" , sparse@vger.kernel.org Subject: Re: Arrays of variable length References: Date: Sun, 05 Mar 2017 14:27:11 +0000 In-Reply-To: (Tomas Winkler's message of "Sun, 5 Mar 2017 11:44:33 +0200") Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8BIT Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Tomas Winkler writes: > Sparse complains for arrays declared with variable length > > 'warning: Variable length array is used' > > Prior to c99 this was not allowed but lgcc (c99) doesn't have problem > with that https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html. > And also Linux kernel compilation with W=1 doesn't complain. > > Since sparse is used extensively would like to ask what is the correct > usage of arrays of variable length > within Linux Kernel. Variable-length arrays are a very bad idea. Don't use them, ever. If the size has a sane upper bound, just use that value statically. Otherwise, you have a stack overflow waiting to happen and should be using some kind of dynamic allocation instead. Furthermore, use of VLAs generally results in less efficient code. For instance, it forces gcc to waste a register for the frame pointer, and it often prevents inlining. -- Måns Rullgård