简体   繁体   中英

gcc consumes all memory when optimizing -O3

I tried to compile the following function to see what gcc made of it:

#include <stdint.h>
#include <stddef.h>

typedef struct giga
{
  uint64_t g[0x10000000];
} giga;

uint64_t addfst(giga const *gptr, size_t num)
{
  uint64_t retval = 0;
  for (size_t i = 0; i < num; i++)
    {
      retval += gptr[i].g[0];
    }
  return retval;
}

And found gcc maxing out my memory, swapping itself to death.
I've found this to happen when optimizing at -O3 , haven't tried to dissect the exact flag(s) responsible. Testing the function on gcc.godbolt reveals this to be gcc specific, but afflicting 4.8 and 4.9 versions.
Is this a genuine compiler bug, or is my function broken?

The bug is on the gcc bugzilla, https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65518 . It has been confirmed & apparently fixed in trunk. Here's hoping the fix eventually trickles down to my distro. Thanks everyone!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM