我的函数myAlloc()遇到一些困难。第一次调用它时效果很好,但是当我第二次调用它时,它在我的输出“ malloc()中显示此消息:损坏的最大大小已中止(内核已转储)”。 如下所示,问题似乎出在malloc()
typedef struct {
int a[128][65536/128];
int b[128];
}pagingTable;
int * myAlloc(pagingTable *mp, int sz){
...
printf("test1\n");
int *mark = malloc(sizeof(int) * myVariable); // 0<myVariable<129
printf("test2\n");
memset(mark, 0, sizeof(int) * myVariable);
...
return mark;
}
int main(){
paging table *mem = initTablePaging
int *adr1;
adr1 = myAlloc(mem, 5);
int *adr2;
adr2 = myAlloc(mem, 10)
free(adr2);
free(adr1);
free(mem);
}
完整功能myAlloc()可能会帮助
typedef int address_PT
address_PT * myAlloc(pagingTable *mp, int sz){
//int count = 0;
int i = 0, j = 0, enough = 0;
//int allGood = 1;
int mnyPage = 1;
int taille = sz - PAGE;
//first, find how many pages we need
for(i=0 ; i<VPN ; i++){
if(taille < 0) { break; }
else {
mnyPage++;
taille -= PAGE;
}
}
//initialize an array where we'll save the adresses of the needed pages
printf("test1\n");
address_PT *mark = (address_PT*)malloc(sizeof(address_PT) * mnyPage);
//address_PT mark[mnyPage];
printf("test2\n");
memset(mark, 0, sizeof(address_PT) * mnyPage);
//make sure we use these variables again
i=0;
taille=0;
//then try to see if we have enough free pages
while(taille < mnyPage){
for(i = 0 ; i< VPN ; i++){
if(mp->pageTable[i] == PAGE) {
taille ++;
mark[j] = i;
j++;
}
}
}
if(taille >= mnyPage){enough = 1;}
//make sure we use these variables again
i=0;
j=0;
taille=0;
//if enough free pages
if (enough){
for(i=0 ; i<mnyPage ; i++)
for(j=0 ; j<sz ; j++){
mp->table[mark[i]][j] = 1;
//update available space in struct
mp->pageTable[mark[i]]--;
//handle situation if sz>PAGE
if(j == PAGE-1){
j=0;
sz-=PAGE;
i++;
}
}
}
return mark;
}
在此先感谢您为我提供的任何帮助!