When does C 'override a local stack variable?

Local variables exist at least (and no more) inside functions. However, what happens to the block variables outside the block, but this is one and the same function, can I save and use their address? Is this code valid?

#include <stdio.h>

int main()
{
   char *f;
   if (1)
   {
       char q[] = "123";
       f = q;   
   }

   printf ("%s\n", f);   
   return 0;
}

Actually gcc -ansi -pedanticneither valgrind nor complain about it, but can I use its cross-platform and cross-compiler? It seems to me not, but which tool can show me the error?

PS Should I use staticin the end? It may be a suitable solution, but it does not seem to be thread safe?

+4
source share
1 answer

, . , . , , - , undefined.

[C11 §6.2.4 ¶2]

- , . , , 33) . 34) , undefined. , , ( ), .

[C11 §6.2.4 ¶6]

( ), , , , , - .

+9

Source: https://habr.com/ru/post/1675028/


All Articles