2011-11-02 72 views
1
int *f, *l; 

int main(){ 
    int *a; 
    a = calloc(1, sizeof(int)); 
    f = l = a; 
    put(&a, 1); 
    put(&a, 3); 
    put(&a, 2); 
    _getch(); 
    return 0; 
} 

void put(int **a, int d){ 
    printf("--%d--", sizeof(*a));   //always == 4 
    void *tmp = (int *)realloc(*a, sizeof(*a) + sizeof(int)); 
    if (temp)        //allocated succesfully 
     *a = temp; 
    else 
     printf("Allocating a failed"); 
    l++; 
} 

我試圖創建一個基於int指針的隊列模型。realloc不起作用

我已經糾正了一下這個例子。但它仍然失敗。 你能幫忙嗎?

+0

'tmp'或'temp'? – qrdl

+0

你永遠不會引用參數「d」...?你的代碼並沒有真正做任何事情。每個realloc與最後一個尺寸相同,因此不會執行任何操作,而是返回原始指針。 – noelicus

回答

0

您正在修改函數中的局部變量a,而不是主函數中的變量a。您可能需要從put()返回一個新的值,或者將指針傳遞給指針(int ** a)以修改它。

例如:

int *put(int *a, int d); 

int main(){ 
    int *a; 
    a = calloc(1, sizeof(int)); 
    a = put(a, 1); 
    ... 
} 

int *put(int *a, int d){ 
    void *tmp = (int *)realloc(a, sizeof(a) + sizeof(int)); 
    if (tmp) 
     a = tmp;  //allocated succesfully 
    else 
     printf("Reallocating of 'a' size %d failed\n", sizeof(a)); 
    return a; 
} 

的sizeof(一)將永遠在你的情況下返回4。它返回指針的大小,而不是指針指向的內存大小。

1

a是一個int指針(int *),因此它的大小如果4字節(在你的機器上)你應該跟蹤分配內存的大小。

例如:

int *f, *l; 

int main(){ 
    int *a; 
    size_tasize = 0; 
    a = calloc(1, sizeof(int)); 
    f = l = a; 
    asize = sizeof(int); 
    put(a, 1, &asize); 
    put(a, 3, &asize); 
    put(a, 2, &asize); 
    _getch(); 
    return 0; 
} 

void put(int *a, int d, size_t * asize){ 
    printf("--%d--\n", asize);  //always == 4 
    void *tmp = (int *)realloc(a, *asize + sizeof(int)); 
    (*asize) += 4; 
    if (tmp) 
     a = tmp;  //allocated succesfully 
    else 
     printf("Reallocating of 'a' size %d failed\n", asize); 
    l++; 
} 
1

在C中,沒有辦法知道這是由一個指針引用的陣列的大小:

int a[25]; // Known size 
int *b = a; // Unknown size 

所以sizeof()只是打印的大小在32位平臺上是4字節的指針。

如果你需要的大小,分配結構,像這樣:

struct Mem { 
    int size; 
    int a[1]; 
} 

使用sizeof(struct Mem) + sizeof(int) * amount,以確定有多少內存來分配,其分配給一個指針。現在您可以使用ptr->a[x]的內存。

注意,它會分配更多的內存是必要的(通常爲4個字節),但這種方法適用於不同的路線,指針大小等

1

sizeof(a)是指針,不是a點的大小。

0

而不是做

if (tmp) 
    a = tmp; 

回報tmp,並在主要分配給a的。

+0

這還不夠;還有一些數據需要輸入。 – glglgl

0

如果你想在它已被定義其他然後一個一個函數來重新分配一個新塊的指針,你有一個指針傳遞給這個指針或返回新分配的塊並將其收集到在調用者函數中相同的舊塊,否則你會更新副本。

0

整個概念不會按照您所需的方式工作。

  • sizeof a東西,不工作,你打算的方式。
  • 重新分配本身是錯誤的,因爲您不會將新地址返回給調用者。
  • 您沒有關於數據長度的信息。

我想提出以下幾點建議:

struct memblock { 
    unsigned int alloced; 
    unsigned int len; 
    int * data; 
} 

// in order to prealloc 
char add_realloc(struct memblock * mb, unsigned int add) { 
    add += mb->alloced; 
    int * tmp = realloc(mb->data, sizeof(*mb) + add * sizeof(*(mb->data))); 
    if (!tmp) return 0; 
    mb->data = tmp; 
    mb->alloced = add; 
    return 1; 
} 

char put(struct memblock * mb, int d) { 
    if (mb->len == mb->alloced) { 
     // realloc 
     if (!add_realloc(mb, 1)) return 0; 
    } 
    mb->data[mb->len++] = d; 
    return 1; 
} 

int main(){ 
    struct memblock a = {} // init with all zeros. 
    // Calling realloc() with a NULL pointer is like malloc(). 

    // we put 3 values. Prealloc for not to have to realloc too often. 
    if (add_realloc(&a, 3) { 
     // now we are safe. Don't check the return values - it is guaranteed to be ok. 
     put(&a, 1); 
     put(&a, 3); 
     put(&a, 2); 
    } 

    return 0; 
}