1

I am new to C and am trying to make an array of one million random __uint128_ts. Here is my failed attempt:

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <time.h>
#include <string.h>


void print128(__uint128_t u) {
  if (u>9) print128(u/10);
  putchar(48+(int)(u%10));
}

typedef __uint128_t u128;
uint64_t wyhash64_x;


uint64_t wyhash64() {
  wyhash64_x += 0x60bee2bee120fc15;
  __uint128_t tmp;
  tmp = (__uint128_t) wyhash64_x * 0xa3b195354a39b70d;
  uint64_t m1 = (tmp >> 64) ^ tmp;
  tmp = (__uint128_t)m1 * 0x1b03738712fad5c9;
  uint64_t m2 = (tmp >> 64) ^ tmp;
  return m2;
}

int main() {
  u128 vals128[1000000];
  u128 lower;
  u128 higher;

  for (int i = 0; i < 1000000; i++) {
    lower = (u128) wyhash64();
    higher = (u128) wyhash64();
    higher = higher << 64;
    vals128[i] = lower + higher;
  }
    print128(vals128[0]);
    printf("\n"); 
}

This compiles with no warnings but segfaults when I run it.

What am I doing wrong?

Simd
  • 19,447
  • 42
  • 136
  • 271
  • 1
    Not enough stack for the local array? `u128 vals128[1000000]` will need 16,000,000 bytes. – Weather Vane Jul 31 '21 at 18:06
  • 1
    So you want someone will run the code in the debugger for you to locate the problem? – folibis Jul 31 '21 at 18:07
  • @folibis An alternative is to explain how to run the code in the debugger. In this case I tried it and it didn't tell me that I had run out of stack that I could understand. – Simd Jul 31 '21 at 18:13
  • @WeatherVane Yes you are right. – Simd Jul 31 '21 at 18:14
  • 1
    @folibis In fairness to OP, this is the sort of problem that's hard to debug for a beginner because the program can crash before it gets a chance to run. Easy to miss if you're inexperienced. – Daniel Kleinstein Jul 31 '21 at 18:16
  • One thing that is wrong here, is that OP fails to mention whether the code worked when making e.g. 10 random numbers. Programming in C can be like handling a loaded gun. Don't make assumptions, especially about memory allocation. – Cheatah Jul 31 '21 at 18:29
  • @SteveSummit 1) is what I already do isn't it? Would 2) also fix the problem? – Simd Jul 31 '21 at 18:35
  • @donald Sorry, left off the most important part. Reposted my comment below. But, yes, (2) would also work. (No typo there.) – Steve Summit Jul 31 '21 at 18:41
  • 1
    Three options: (1) `static u128 vals128[1000000];` (2) move `vals128` outside of `main`; (3) call `malloc`, as in Fract's answer. – Steve Summit Jul 31 '21 at 18:41

1 Answers1

4

Declaring an array of more than size 1,048,576 bytes locally is a bad idea since the stack size is generally 1,048,576 (2^20).

Instead, use u128* vals128 = malloc(1000000 * sizeof(*vals128));

Daniel Kleinstein
  • 5,262
  • 1
  • 22
  • 39
Fract
  • 113
  • 6