c - Relocation error -
#include <stdio.h> #define max 1000000 int dp[max]; int p[max], c[max], k[max], child[max][1000], index[max]; int mod = 1000000007; void dfs(int i) { int j = 1; while (j <= index[i]) { dfs(child[i][j]); if ((c[child[i][j]] == c[i]) && (k[i] - k[child[i][j]] == 1)) dp[i] = (dp[i] % mod + dp[child[i][j]] % mod) % mod; ++j; } } int main() { int t, n, x, i, j; scanf("%d", &t); while (t--) { scanf("%d%d", &n, &x); (i = 0; < n; ++i) index[i] = 0; (i = 1; < n; ++i) { scanf("%d", &p[i]); child[p[i]][++index[p[i]]] = i; } (i = 0; < n; ++i) scanf("%d", &c[i]); (i = 0; < n; ++i) { scanf("%d", &k[i]); if (k[i]) dp[i] = 0; else dp[i] = 1; } dfs(0); (i = 0; < n; ++i) printf("%d\n", dp[i]); } return 0; } when compiled above code, got following error:
in function `dfs': (.text+0xa): relocation truncated fit: r_x86_64_32s against symbol `index' defined in common section in /tmp/cc0vmxet.o (.text+0x3b): relocation truncated fit: r_x86_64_32s against symbol `index' defined in common section in /tmp/cc0vmxet.o (.text+0x4f): relocation truncated fit: r_x86_64_32s against symbol `c' defined in common section in /tmp/cc0vmxet.o (.text+0x56): relocation truncated fit: r_x86_64_32s against symbol `c' defined in common section in /tmp/cc0vmxet.o (.text+0x60): relocation truncated fit: r_x86_64_32s against symbol `k' defined in common section in /tmp/cc0vmxet.o (.text+0x67): relocation truncated fit: r_x86_64_32s against symbol `k' defined in common section in /tmp/cc0vmxet.o (.text+0x74): relocation truncated fit: r_x86_64_32s against symbol `dp' defined in common section in /tmp/cc0vmxet.o (.text+0x84): relocation truncated fit: r_x86_64_32s against symbol `dp' defined in common section in /tmp/cc0vmxet.o (.text+0x97): relocation truncated fit: r_x86_64_32s against symbol `dp' defined in common section in /tmp/cc0vmxet.o in function `main': (.text.startup+0x6e): relocation truncated fit: r_x86_64_32s against symbol `index' defined in common section in /tmp/cc0vmxet.o (.text.startup+0xba): additional relocation overflows omitted output error: ld returned 1 exit status i know error is;how occur. have searched in stackoverflow, can't understand how correct it. can please tell me how correct code?
you may have hit limit on size of global variables defined in program: 2d array child alone has size of 4000000000 bytes (4 billion bytes).
either reduce size of of these global variables, or allocate them heap @ start time calloc().
try version, child allocated heap:
#include <stdio.h> #include <stdlib.h> #define max 1000000 int dp[max], p[max], c[max], k[max], index[max]; int (*child)[1000]; int mod = 1000000007; void dfs(int i) { int j = 1; while (j <= index[i]) { dfs(child[i][j]); if ((c[child[i][j]] == c[i]) && (k[i] - k[child[i][j]] == 1)) dp[i] = (dp[i] % mod + dp[child[i][j]] % mod) % mod; ++j; } } int main(void) { int t, n, x, i; child = calloc(max, sizeof(*child)); if (child == null) { fprintf(stderr, "cannot allocate child array (%llu bytes)\n", (unsigned long long)max * sizeof(*child)); return 1; } scanf("%d", &t); while (t--) { scanf("%d%d", &n, &x); (i = 0; < n; ++i) index[i] = 0; (i = 1; < n; ++i) { scanf("%d", &p[i]); child[p[i]][++index[p[i]]] = i; } (i = 0; < n; ++i) scanf("%d", &c[i]); (i = 0; < n; ++i) { scanf("%d", &k[i]); if (k[i]) dp[i] = 0; else dp[i] = 1; } dfs(0); (i = 0; < n; ++i) printf("%d\n", dp[i]); } free(child); return 0; } notes:
you should check if values read standard input compatible allocated sizes (
n <= max,p[i] < 1000).you can reduce
maxor1000if allocation fails.dp[i] = (dp[i] % mod + dp[child[i][j]] % mod) % mod;simplifieddp[i] = (dp[i] + dp[child[i][j]]) % mod;
Comments
Post a Comment