5

I am encountering something extremely strange on my system right now.

#include <cstdint>
#include <iostream>

constexpr size_t N = 1l<<31;

int main() {
    auto buf = new int64_t[N]{}; // value-initialization zeroes the array
    for (size_t i=0; i<N;i++) {
        if (buf[i]!=0) {
            std::cout << "buf[" << i << "]=" << buf[i] << std::endl;
            break;
        }
    }
    delete[] buf;
}

Compiled with g++ -O3 on gcc version 13.3.0 (Ubuntu 13.3.0-6ubuntu2~24.04), the above code keeps producing the result buf[x]=131072 where x is some random index that changes between runs. The value is almost always the above, which is 2^17, but occasionally it is 2^18 instead.

Since these numbers are powers of two, 128k and 256k respectively, I'm suspecting it's some kind of OS or memory paging glitch? Or it might be that my DRAM has some frozen bits? Is there some arcane limitation in g++ about initializing very large arrays?

12
  • Does changing 1l<<31; to 1ul<<31; fix it ? Commented Aug 16, 2025 at 19:22
  • 1
    And what happens with std::vector<int> buf(N,0) ? (which anyway would be more in line with the C++ core guidelines) Commented Aug 16, 2025 at 19:58
  • 1
    Side note last time I ever seen something like this happen was on a silicon graphics workstation back in '95. It would happen sporadically if memory was allocated across a page boundary (the os would only really commit the memory page once you accessed it) Commented Aug 16, 2025 at 19:59
  • 1
    @AndrasDeak--СлаваУкраїні I tried that, and in that case, the second read returns zero. I was thinking it might be because of the cache, and the write never reaching the possible defective memory before being read again. Commented Aug 16, 2025 at 20:26
  • 1
    Additionally: now my login shell is giving me: squashfs error: xz decompression failed data probably corrupt ... Commented Aug 16, 2025 at 20:27

1 Answer 1

9

Memtest86+ immediately confirmed a DRAM error in which exactly bit 17 of the result is incorrect, among several other defects. Nonetheless, thanks to everybody who replied in the comments trying to help.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.