Bug 63728 - Memory exhaustion using constexpr constructors for classes with large array members
Summary: Memory exhaustion using constexpr constructors for classes with large array m...
Status: RESOLVED DUPLICATE of bug 56671
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 5.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2014-11-04 01:29 UTC by Benno Evers
Modified: 2016-05-18 03:59 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 4.9.3, 5.3.0, 6.1.0, 7.0
Last reconfirmed: 2016-05-13 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Benno Evers 2014-11-04 01:29:48 UTC
Having a constexpr constructor on a class with a large array member will result in excessive memory usage. For example, I wasn't able to compile the following program using -std=c++11 on a machine with 4GiB of RAM:

#include <bitset>
int main() {
    std::bitset<2147483648> bs; 
}

This happens because the constructor of bitset is constexpr, so it will try to initialize the large member array at compile time, which will create at least one ast tree node for every element in the array. (gcc/cp/constexpr.c:1874)
Comment 1 Martin Sebor 2016-05-13 21:46:51 UTC
Confirmed.  Unbounded constexpr can eat up a lot of memory.  Here's a smaller test case:

$ (cat xx.cpp && ulimit -v $(expr 4096 \* 16) && /build/gcc-trunk-svn/gcc/xg++ -B /build/gcc-trunk-svn/gcc -DNBITS=10000 -S -Wall -Wextra  xx.cpp); echo $?
template <int N>
struct S
{
    int a [N / (8 * sizeof (int))];
    constexpr S (): a () { }
};

S<NBITS> s;
0

$ (ulimit -v $(expr 4096 \* 16) && /build/gcc-trunk-svn/gcc/xg++ -B /build/gcc-trunk-svn/gcc -DNBITS=10000000 -S -Wall -Wextra  xx.cpp); echo $?virtual memory exhausted: Cannot allocate memory
1
Comment 2 Martin Sebor 2016-05-18 03:59:00 UTC
I believe this can actually be considered a duplicate of bug 56671.

*** This bug has been marked as a duplicate of bug 56671 ***