アップデート:私は、私は理解し考え、それ以下のフィードバックごとに、次のように私は、コードを修正しましたが、それはまだ面倒です:ここで私のmalloc/reallocに問題がありますか?
以下unsigned int count = 0;
char* filebuffer;
filebuffer = malloc(sizeof(char));
if (!filebuffer)
{
error(500);
return false;
}
while (fread(filebuffer, sizeof(char), 1, file) == 1)
{
count++;
filebuffer = realloc(filebuffer, count * sizeof(char));
printf("%lu\n", (count + 1) * sizeof(char));
}
if (feof(file))
{
*content = filebuffer;
*length = count;
}
はファイルを通過することを意図しているいくつかのコードですこれはpopen(PHPファイル)によって関数にパイプされ、バッファに格納され、次に同じ内容のポインタと* lengthに読み込まれたバイト数を与えます。
ただし、動作しません。 Valgrindのは言う:
==7608== Conditional jump or move depends on uninitialised value(s)
==7608== at 0x4C31FCE: strstr (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7608== by 0x4036C0: interpret (server.c:513)
==7608== by 0x401D66: main (server.c:259)
==7608== Uninitialised value was created by a heap allocation
==7608== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7608== by 0x4C2CF1F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7608== by 0x40418C: load (server.c:662)
==7608== by 0x403672: interpret (server.c:502)
==7608== by 0x401D66: main (server.c:259)
コードは次のとおりです。事前に
unsigned int count = 0;
char* filebuffer;
filebuffer = malloc(sizeof(char));
if (!filebuffer)
{
printf("oh noes\n");
error(500);
return false;
}
while (fread(filebuffer, sizeof(char), 1, file) == 1)
{
count++;
filebuffer = realloc(NULL, sizeof(filebuffer) + sizeof(char));
}
if (feof(file))
{
*content = filebuffer;
*length = count;
}
任意のフィードバックを歓迎と感謝。
filebuffer = realloc(NULL、sizeof(filebuffer)+ sizeof(char)))を使用する必要があります。これは面倒です – dlmeetei
'realloc'エラーに加えて、' valgrind'の古いバージョン( '〜3.10'など)を使用している場合、' filebuffer = malloc(sizeof(char)); 'は*条件ジャンプまたは移動は、初期化されていない値(*)に依存します。それを修正するには 'filebuffer = calloc(1、sizeof(char));'( 'malloc'は割り当てられたスペースを初期化せず、' calloc'と 'valgrind'の古いバージョンはこれに間違ってフラグを付けます) –
どの行が行513 ? – immibis