Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Large global array of vectors causing compilation error

Tags:

c++

I have a very simple C++ code (it was a large one, but I stripped it to the essentials), but it's failing to compile. I'm providing all the details below.

The code

#include <vector>
const int SIZE = 43691;
std::vector<int> v[SIZE];

int main() {
  return 0;
}

Compilation command: g++ -std=c++17 code.cpp -o code

Compilation error:

/var/folders/l5/mcv9tnkx66l65t30ypt260r00000gn/T//ccAtIuZq.s:449:29: error: unexpected token in '.section' directive
        .section .data.rel.ro.local
                                   ^

GCC version: gcc version 12.1.0 (Homebrew GCC 12.1.0_1)

Operating system: MacOS Monterey, version 12.3, 64bit architecture (M1)

My findings and remarks:

The constant SIZE isn't random here. I tried many different values, and SIZE = 43691 is the first one that causes the compilation error.

My guess is that it is caused by stack overflow. So I tried to compile using the flag -Wl,-stack_size,0x20000000, and also tried ulimit -s 65520. But neither of them has any effect on the issue, the code still fails to compile once SIZE exceeds 43690.

I also calculated the amount of stack memory this code consumes when SIZE = 43690. AFAIK, vectors use 24B stack memory, so the total comes to 24B * 43690 = 1048560B. This number is very close to 220 = 1048576. In fact, SIZE = 43691 is the first time that the consumed stack memory exceeds 220B. Unless this is quite some coincidence, my stack memory is somehow limited to 220B = 2M. If that really is the case, I still cannot find any way to increase it via the compilation command arguments. All the solutions in the internet leads to the stack_size linker argument, but it doesn't seem to work on my machine. I'm wondering now if it's because of the M1 chip somehow.

I'm aware that I can change this code to use vector of vectors to consume memory from the heap, but I have to deal with other's codes very often who are used to coding like this.

Let me know if I need to provide any more details. I've been stuck with this the whole day. Any help would be extremely appreciated. Thanks in advance!

like image 891
Nayeemul Islam Swad Avatar asked Oct 29 '25 19:10

Nayeemul Islam Swad


1 Answers

It does seem to be an M1 / M1 Pro issue. I tested your code on two seperate M1 Pro machine with the same result as yours. One workaround I found is to use the x86_64 version of gcc under rosetta, which doesn't have these allocation problems.

like image 56
Ray Kong Avatar answered Nov 01 '25 08:11

Ray Kong