Realloc()在我的动态数组中引入了随机值

时间:2018-08-07 13:52:03

标签: c arrays dynamic-memory-allocation dynamic-arrays realloc

更新:不重复。正如Kamil Cuk在评论中指出的那样,我应该使用memcpy()而不是分配指针!谢谢您的时间

我正在使用<div className='DropDownButton2'> <label style={{marginTop: '20px'}}>Second</label> <br/> <select defaultValue={-1} style={{width: '300px', height: '35px'}}> <option disabled value={-1}> </option> <option value='One'>One</option> <option value='Two'>Two</option> <option value='Three'>Three</option> </select> </div> <div className='DropDownButton3'> <label className='hide' style={{marginTop: '20px'}}>Third</label> <br/> <select className='hide' defaultValue={-1}> <option disabled value={-1}> </option> <option value='A'>A</option> <option value='B'>B</option> </select> </div> .css .DropDownButton2 { grid-column: 3; grid-row: 2/3; } .DropDownButton3 { grid-column: 3; grid-row: 3; } 来缩放动态数组。在函数realloc()中,我添加了正确的值以纠正指针(检查程序输出),但是当我使用addToList()并打印出先前分配的所有值时,其中大多数是正确的,但有些是错误的(检查输出) ),奇怪的是,错误值的数量等于使用getFromList()的次数,但这些值是完全随机的间隔! (realloc()成功退出)。我不知道为什么会弹出这些随机值,我们将不胜感激

调用这些函数:

realloc()

函数定义:

//TEST
arrayList list;
if(initList(&list,sizeof(int))!=0){
    printf("List init failed");
    exit(1);
}
int tempNum = 38;
int i = 0;
while(1){
    tempNum+=1;
    if(addToList(&list,&tempNum)!=0){
        printf("Failed to add to list");
        exit(1);
    }
    //sleep(1);

    //Break after 31 values assigned
    if(i>=30){
        break;
    }
    i++;
}
//Print the 31 values previously assigned
for(int i = 0;i<=30;i++){
    //Get data at index
    int *num = NULL;
    num = getFromList(&list,i);
    //Check errors
    if(num==NULL){
        //ERROR
        exit(1);
    }
    //Print data
    printf("Data: %i  ",*num);
    printf("Location in memory: %i\n", num);
}

arrayList typedef:

int initList(arrayList *arrayLst, size_t dataSize){
    printf("Initializing a list\n");
    //Allocate memory for list struct itself
    arrayLst = memset(arrayLst,0, sizeof(arrayLst));

    //Allocate memory for first block (depends on increments defined in .h file)
    char *tempDataMallocPtr;
    if((tempDataMallocPtr = malloc((arrayLst->dataSize)*listIncrement))==NULL){
        return 1;
    }
    arrayLst->data = tempDataMallocPtr;

    //Initialize values
    arrayLst->length = 0;
    arrayLst->dataSize = dataSize;
    arrayLst->allocated = listIncrement;

    return 0;

}

int addToList(arrayList *arrayLst, void *data){

    //Check if enough space
    if(arrayLst->allocated > arrayLst->length){
        //Enough memory
        //printf("Enough memory\n");
    }else{
        //Reallocating memory
        printf("Not enough memory reallocating\n");
        printf("New memory size = %i\n", ((arrayLst->allocated)*(arrayLst->dataSize) + (arrayLst->dataSize)*listIncrement));
        fflush(stdout);
        int newSize = (arrayLst->allocated)*(arrayLst->dataSize) + (arrayLst->dataSize)*listIncrement;
        char *tempReallocPtr;
        //Realloc() error checking
        if ((tempReallocPtr = realloc((arrayLst->data), newSize))==NULL){
            printf("Realloc failed in arrayList\n");
            return 1;
        }
        arrayLst->data = tempReallocPtr;
        arrayLst->allocated = newSize/(arrayLst->dataSize);
    }

    //Get pointer for last data index and set it to data provided
    printf("Adding data at pointer: Pointer address = %i    ", (arrayLst->data)+((arrayLst->length)*(arrayLst->dataSize)));
    printf("data being added %i    ",*((char*)data));
    *((arrayLst->data)+((arrayLst->length)*(arrayLst->dataSize))) = *((char*)data);
    (arrayLst->length)+=1;

    printf("Added to list\n");
    //Success
    return 0;

}

void* getFromList(arrayList *arrayLst, int index){
    //Check if index correct
    if(index<(arrayLst->length) && index>=0){
        //Correct
        return (arrayLst->data+index*(arrayLst->dataSize));
    }else{
        //ERROR
        fprintf(stderr, "Wrong index in arrayList\n");
        return NULL;
    }
}

输出:

typedef struct _arrayList{
    char *data;
    size_t dataSize;
    int allocated; //Memory allocated
    int length;  //Numbers inside the list 
}arrayList;

1 个答案:

答案 0 :(得分:0)

如果您通过malloc分配内存,或者通过realloc增加内存量,则(新)内存的内容不确定,并且看似随机。除了初始化外,请勿使用它。