Re: Two questions on arrays with size defined by variables

Liste des GroupesRevenir à cl c 
Sujet : Re: Two questions on arrays with size defined by variables
De : ifonly (at) *nospam* youknew.org (Opus)
Groupes : comp.lang.c
Date : 10. Feb 2025, 23:08:35
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vodtd3$1d8fj$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10
User-Agent : Mozilla Thunderbird
On 10/02/2025 11:38, Michael S wrote:
On Mon, 10 Feb 2025 02:44:26 +0100
Opus <ifonly@youknew.org> wrote:
 
On 09/02/2025 19:19, Michael S wrote:
On Sun, 9 Feb 2025 18:46:44 +0100
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
  
wrong! - assignment to 'a[99]' produced also no compiler
complaints,
>
gcc produces warning in this case, but only at optimization level
of 2 or higher.
>
Which version of gcc?
 14.1
 My test code is:
 void bar(char*[]);
int foo(void)
{
   int n = 10;
   char *a[n];
   bar(a);
   a[99] = "42";
   return a[3][2];
}
 
Tried with gcc 14.2 (x86-64) with -Wall -O3 (or -O2, same), it
doesn't give any warning whatsoever. (And yes, same with clang.)
>
 May be, in your test arr[] is not used later, so compiler silently
optimizes away all accesses?
You're right. I was actually using arr[] to avoid this pitfall, but the way I used it was not 'reading' arr[99], and so the compiler did what you say.
Note that with your example, if you comment out the bar() call, you won't get the warning (but you'll get another one about a[3][2] being used uninitialized). And as you did, declaring an external function bar() guarantees that the compiler can't guess what it does, so that prevents any further optimization on the array access.
The underlying "issue" is that gcc analyzes code after the optimization pass (or it does remove warnings that it detected before optimization on 'dead code'). This may be defended. There are quite a few tickets on their bugzilla about this though, because it tends to surprise many people, and while the example above is relatively simple, there are tons of potential cases where it's way less trivial to understand.
I would personally favor giving a warning even if the code is optimized away during optimization, and actually mentioning that the statement has no effect (after optimization). I'm not sure the GCC team either cares, or that it's even doable considering the architecture of the compiler. Just a thought.
As I said, don't hesitate to use a third-party static analyzer to complement compiler warnings.

Date Sujet#  Auteur
9 Feb 25 * Two questions on arrays with size defined by variables45Janis Papanagnou
9 Feb 25 `* Re: Two questions on arrays with size defined by variables44Andrey Tarasevich
9 Feb 25  +* Re: Two questions on arrays with size defined by variables41Janis Papanagnou
9 Feb 25  i+* Re: Two questions on arrays with size defined by variables10Keith Thompson
9 Feb 25  ii`* Re: Two questions on arrays with size defined by variables9Janis Papanagnou
10 Feb 25  ii `* Re: Two questions on arrays with size defined by variables8Keith Thompson
10 Feb 25  ii  `* Re: Two questions on arrays with size defined by variables7Janis Papanagnou
10 Feb 25  ii   +- Re: Two questions on arrays with size defined by variables1James Kuyper
10 Feb 25  ii   `* Re: Two questions on arrays with size defined by variables5Andrey Tarasevich
10 Feb 25  ii    `* Re: Two questions on arrays with size defined by variables4Janis Papanagnou
10 Feb 25  ii     `* Re: Two questions on arrays with size defined by variables3Keith Thompson
10 Feb 25  ii      `* Re: Two questions on arrays with size defined by variables2Janis Papanagnou
10 Feb 25  ii       `- Re: Two questions on arrays with size defined by variables1Keith Thompson
9 Feb 25  i+* Re: Two questions on arrays with size defined by variables6Michael S
9 Feb 25  ii`* Re: Two questions on arrays with size defined by variables5Janis Papanagnou
9 Feb 25  ii `* Re: Two questions on arrays with size defined by variables4Michael S
9 Feb 25  ii  +- Re: Two questions on arrays with size defined by variables1Janis Papanagnou
10 Feb 25  ii  `* Re: Two questions on arrays with size defined by variables2Keith Thompson
10 Feb 25  ii   `- Re: Two questions on arrays with size defined by variables1Janis Papanagnou
9 Feb 25  i+* Re: Two questions on arrays with size defined by variables20Waldek Hebisch
9 Feb 25  ii`* Re: Two questions on arrays with size defined by variables19Janis Papanagnou
9 Feb 25  ii +* Re: Two questions on arrays with size defined by variables13Andrey Tarasevich
9 Feb 25  ii i`* Re: Two questions on arrays with size defined by variables12Janis Papanagnou
9 Feb 25  ii i +* Re: Two questions on arrays with size defined by variables7Janis Papanagnou
9 Feb 25  ii i i`* Re: Two questions on arrays with size defined by variables6James Kuyper
10 Feb 25  ii i i +- Re: Two questions on arrays with size defined by variables1Waldek Hebisch
10 Feb 25  ii i i `* Re: Two questions on arrays with size defined by variables4Janis Papanagnou
10 Feb 25  ii i i  `* Re: Two questions on arrays with size defined by variables3David Brown
10 Feb 25  ii i i   `* Re: Two questions on arrays with size defined by variables2Janis Papanagnou
10 Feb 25  ii i i    `- Re: Two questions on arrays with size defined by variables1David Brown
9 Feb 25  ii i `* Re: Two questions on arrays with size defined by variables4Michael S
10 Feb 25  ii i  `* Re: Two questions on arrays with size defined by variables3Opus
10 Feb 25  ii i   `* Re: Two questions on arrays with size defined by variables2Michael S
10 Feb 25  ii i    `- Re: Two questions on arrays with size defined by variables1Opus
10 Feb 25  ii `* Re: Two questions on arrays with size defined by variables5Keith Thompson
10 Feb 25  ii  `* Re: Two questions on arrays with size defined by variables4Janis Papanagnou
10 Feb 25  ii   `* Re: Two questions on arrays with size defined by variables3Janis Papanagnou
10 Feb 25  ii    `* Re: Two questions on arrays with size defined by variables2Keith Thompson
10 Feb 25  ii     `- Re: Two questions on arrays with size defined by variables1Janis Papanagnou
9 Feb 25  i+* Re: Two questions on arrays with size defined by variables3Andrey Tarasevich
9 Feb 25  ii+- Re: Two questions on arrays with size defined by variables1Janis Papanagnou
9 Feb 25  ii`- Re: Two questions on arrays with size defined by variables1James Kuyper
9 Feb 25  i`- Re: Two questions on arrays with size defined by variables1James Kuyper
9 Feb 25  +- Re: Two questions on arrays with size defined by variables1James Kuyper
15 Feb17:19  `- Re: Two questions on arrays with size defined by variables1Tim Rentsch

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal