Nayeemul Islam Swad
Nayeemul Islam Swad

Reputation: 157

Large global array of vectors causing compilation error

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!

Upvotes: 4

Views: 790

Answers (4)

I had the same issue, and after adding the -O2 flag to the compilation command, it started working. No idea why.

So, something like this:

g++-12 -O2 -std=c++17 code.cpp -o code

Upvotes: 2

asdx
asdx

Reputation: 1

This is a gcc-12 problem on Darwin Aarch64 targets. It shall not emit such sections like .section .data.rel.ro.local. Section names on macOS shall start with __, eg.: .section __DATA,... See Mach-O reference.

Upvotes: 0

James Risner
James Risner

Reputation: 6074

Works on a M1 Max running Monterey 12.5.1 with XCode 13.4.1 and using clang 13.1.6 compiler:

% cat code.cpp
#include <vector>
const int SIZE = 43691;
std::vector<int> v[SIZE];

int main() {
  return 0;
}
% cc -std=c++17 code.cpp -o code -lc++
% ./code

Also fails with gcc-12.2.0:

% g++-12 -std=c++17 code.cpp -o code
/var/tmp/ccEnyMCk.s:449:29: error: unexpected token in '.section' directive
        .section .data.rel.ro.local
                                   ^

So it seems to be a gcc issue on M1 issue.

Upvotes: 0

Ray Kong
Ray Kong

Reputation: 36

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.

Upvotes: 2

Related Questions