This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Re: Why does gcc generate const local array on stack?

See this example:

In this example the function is called recursively.
During each call a pointer to that local areay is appended to a static array of pointers.
Should a new instance of that local array of const int be created every time, abort() will never be called.
Since calling a library function is observable behavior, clang's optimization has effectively changed that program's behavior. Hence I think it is wrong.

#include <stdlib.h>

static const int *ptrs[2];
static unsigned recur;

void foo(){
  const int a[] = {0,1,2,3,4,5,6,7,8,9};
  ptrs[recur] = a;
  if(recur == 0){
  if(ptrs[0] == ptrs[1]){

int main(){

Best regards,

åääïJonathan Wakely <>
åéææï2016-04-21 01:51
æéïBingfeng Mei,gcc
äéïRe: Why does gcc generate const local array on stack?

On 20 April 2016 at 18:31, lh_mouse wrote:
> I tend to say clang is wrong here.

If you can't detect the difference then it is a valid transformation.

> Your identifier 'a' has no linkage. Your object designated by 'a' does not have a storage-class specifier.
> So it has automatic storage duration and 6.2.4/7 applies: 'If the scope is entered recursively, a new instance of the object is created each time.'

How do you tell the difference between a const array that is recreated
each time and one that isn't?

> Interesting enough, ISO C doesn't say whether distinct objects should have distinct addresses.
> It is worth noting that this is explicitly forbidden in ISO C++ because distinct complete objects shall have distinct addresses:

If the object's address doesn't escape from the function then I can't
think of a way to tell the difference.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]