我正在使用Malloc創建一個指針數組。不過,我接受valgrind每當我嘗試引用數組中某個索引中的某些內容時,條件跳轉或移動取決於未初始化的值。在我的代碼中,somethimes會在index []處存儲某些內容,有時不會。例如,可能有一個指針存儲在值1,4,6,但不在其他任何其他指針中。我的目標是能夠確定沒有valgrind錯誤!Malloc數組,條件跳轉到未初始化的值
typedef struct{
char* symbol;
void* datapointer;
void* nextstruct;
}Entry;
void main(){
int sizeHint = 10000; //size of array
Entry** arrayOfPointers = malloc(sizeHint * sizeof(Entry*));
//For the sake of keeping this simple, say I stored something
//in a bunch of indexes in the array but NOT at 5
if(arrayOfPointers[5] != NULL){
//this is where my error comes, as it reads
//conditional jump or move depends on uninitilised value
//my goal is to be able to determine if something is stored at an index, and
//do something if its not stored
}
}
您應該使用'calloc()'來代替:它爲您執行乘法,檢查溢出並將內存初始化爲零。 –
@JonathonReinhart:目前的C標準沒有要求所有'0'的位模式會使指針變量等於空指針:http://stackoverflow.com/questions/42471057/malloc-array-conditional -jump-on-uninitialized-values/42471123#comment72082868_42471123 – alk