-1
現在、ナップザックの問題を解決するためのプログラムを作成しています。アドレス0x0はスタックされていない、mallocされていない、または(最近)free'd
1 errors in context 1 of 2:
==10545== Invalid write of size 4
==10545== at 0x4009C3: DP (dp.c:70)
==10545== by 0x400914: main (dp.c:39)
==10545== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==10545==
==10545==
=
=10545== 1 errors in context 2 of 2:
==10545== Use of uninitialised value of size 8
==10545== at 0x4009C3: DP (dp.c:70)
==10545== by 0x400914: main (dp.c:39)
==10545==
私は問題を解決するために、過去の回答を使用しようとしましたが、私は数字に見えることはできません。私は学校で与えられた疑似コードを使用した後、私は、これが理由でセグメンテーションフォールトを受信し、valgrindのに応じてキープそれを出す。学校の他の人たちもまったく同じことをしていて、この問題を受けていないようです。私が見ていないか、コードで実現していないような間違いはありますか?あなたのmalloc
呼び出しで供給
int **V, **keep; // 2d arrays for use in the dynamic programming solution
// keep[][] and V[][] are both of size (n+1)*(W+1)
int i, w, K;
// Dynamically allocate memory for variables V and keep
V = (int**)malloc((n+1) * (W+1) * sizeof(int*));
keep = (int**)malloc((n+1) * (W+1) * sizeof(int*));
// set the values of the zeroth row of the partial solutions table to zero
for (w = 0; w <= W; w++)
{
V[0][w] = 0;
}
// main dynamic programming loops , adding one item at a time and looping through weights from 0 to W
for (i = 1; i <= n; i++)
for (w = 0; w <= W; w++)
if ((wv[i] <= w) && (v[i] + V[i-1][w - wv[i]] > V[i - 1][w]))
{
V[i][w] = v[i] + V[i-1][w-wv[i]];
keep[i][w] = 1;
}
else
{
V[i][w] = V[i-1][w];
keep[i][w] = 0;
}
K = W;
for (i = n; i >= 1; i--);
{
if (keep[i][K] == 1)
{
printf("%d\n", i);
K = K - wv[i];
}
}
return V[n][W];
}
'NULL'ポインタ逆参照です。 –