虽然我认为此代码中有多个问题,但我想先解决编译错误。提前感谢您的任何建议。接下来我会尝试解决运行错误 错误:
fuzzer.c: In function ‘main’:
fuzzer.c:26:25: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
charArray[arraySize-1] = NULL; /*make sure charArray[] is a string array that has a size of arraySize */
^
守则:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <time.h>
int main(void)
{
int fuzzNum = 100;
char buffer[1000], *charArray;
int status, ret, i, j, retCode, arraySize; /* */
time_t t;
FILE *fin;
FILE *fout;
srand((unsigned) time(&t)); /* randomize the initial seed */
for(i=0; i<fuzzNum; i++)
{
charArray = (char *) malloc(arraySize);
for (j=0; j< arraySize; j++)
charArray[j] = 'A';
charArray[arraySize-1] = NULL;
/*make sure charArray[] is a string array that has a size of arraySize */
/* open and read the cross.jpg file as a binary format file*/
fin=fopen("./cross.jpg","rb" );
/*generate a variable file name*/
char fileName[30]; int n;
sprintf(fileName, "crashed-%d.jpg", n);
fout=fopen("./test.jpg","wb");
/* execute the jpg2bmp file to process the test.jpg file*/
char comBuf[200];
sprintf(comBuf, "./jpg2bmp test.jpg temp.bmp");
ret=system(comBuf);
free(charArray); /* must free memory for repeat testing! */
ret=system(buffer);
wait(&status);
retCode=WEXITSTATUS(ret);
if ( retCode == 128+11 || retCode ==128+6) /* segmentation fault (11) or Abort (6) */
{
printf("retCode=%d, arraySize = %d", retCode, arraySize);
fflush(stdout); /*make sure output is print out immediately ! */
}
}
return 0;
}
答案 0 :(得分:0)
首先,这是一个警告,而不是错误。如果此警告是编译器在构建过程中产生的唯一问题,那么您的代码应该至少成功编译。但是,如果您想克服此问题,只需更改:
charArray[arraySize-1] = NULL;
要:
charArray[arraySize-1] = 0;
虽然charArray
是指向数组中第一个内存空间的指针,charArray[i]
,其中i
表示计数器作为示例,但数据类型为{{1} }}。现在,当指针指向任何东西时,或者用计算机术语指向地面时,使用int
。因此,编译器警告您,您正在将指针属性存储在非指针存储空间中(很抱歉,如果有任何问题,请在评论框中纠正我)。
上面的代码更改应解决编译器产生的警告。