2012-12-27 23 views
-1

可能重複:
Using Dynamic Memory allocation for arrays內存分配使用數組

我本來這個程序商店的價格10,而量尺寸,並意識到,我想使程序更加動態化,因爲我可能需要在某個給定點存儲超過10個項目。我很難理解如何重新分配額外的內存,以便我可以存儲任何數量的需要的項目。這是處理這項任務的正確方法嗎?

主要功能

double *purchases = (double*)malloc(QUANTITY_SIZE); 

外功能

double startShopping(double *purchases, double *taxAmount, double *subTotal, double *totalPrice) 
{ 
    double itemPrice = 0.00; 
    double* storeMoreItems; 

    for(int i = 0; i < QUANTITY_SIZE; *subTotal +=purchases[i++]) 
    { 
     while(itemPrice != -1) 
     { 
      printf("Enter the price of the item :"); 
      scanf("%lf", &itemPrice); 

      storeMoreItems = (double*)realloc(storeMoreItems, i * sizeof(int)); 

      if(storeMoreItems != NULL) 
      { 
       storeMoreItems = purchases; 
       purchases[i-1] = itemPrice; 
      } 

      else 
      { 
       free(purchases); 
      } 
     } 
    } 

    displayCart(purchases); 

    *taxAmount = *subTotal * TAX_AMOUNT; 

    *totalPrice = *taxAmount + *subTotal; 

    printf("\nTotal comes to : $%.2lf\n", *totalPrice); 

    return *totalPrice; 
} 

回答

1

double* purchases = (double*)malloc(sizeof(double)*QUANTITY_SIZE);

更何況是:

storeMoreItems = (double*)realloc(storeMoreItems, i * sizeof(double));

您試圖分配i*sizeof(int),然後將其鑄造到double*。當doubleint有不同的大小時,你的代碼將很難找到錯誤。

接下來的事情:

i等於0,你的初始大小爲0字節(i*sizeof(int))分配內存,然後嘗試使用它。它不會工作。嘗試以這種方式更改您的循環:for (int i = 1, i <= QUANTITY_SIZE;...並保留purchases[i-1]

+0

感謝您指出了這一點,我只是糾正了。感謝您的解釋。 – theGrayFox

+0

不客氣;) –

+0

也請看Daniel的回答。他的通知也非常重要。記得給他+1;) –

0

你想要做的第一件事是確保你分配字節的正確數量。 malloc不知道你要那麼你需要sizeof(double)乘用雙打內存:

double *purchases = (double*)malloc(QUANTITY_SIZE * sizeof(double)); 
1

更多的細節這是錯誤的:

 if(storeMoreItems != NULL) 
     { 
      storeMoreItems = purchases; 
      purchases[i-1] = itemPrice; 
     } 

首先,你覆蓋只是realloc ED指針,你的意思是有

purchases = storeMoreItems; 

有代替另一邊。但是這不會影響指針函數中傳入的指針所具有的值。

對於這一點,你需要從main合格purchases地址

double startShopping(double **purchases_ptr, double *taxAmount, double *subTotal, double *totalPrice) 

,並指定

*purchases_ptr = storeMoreItems; 

再分配本身,

storeMoreItems = (double*)realloc(storeMoreItems, i * sizeof(int)); 

使用錯誤鍵入計算分配的大小,即差不多當然也是非常錯誤的。


main

size_t purchase_count = QUANTITY_SIZE; 
double *purchases = malloc(purchase_count * sizeof *purchases); 
// ... 
startShopping(&purchases, &purchase_count, taxAmount, subTotal, totalPrice); 
// ... 

startShopping看起來像

double startShopping(double **purchases_ptr, size_t *purchase_count, 
        double *taxAmount, double *subTotal, double *totalPrice) 
{ 
    double itemPrice = 0.00; 
    double* purchases = *purchases_ptr; 
    size_t i; 

    for(i = 0; ; *subTotal += purchases[i++]) 
    { 
     printf("Enter the price of the item :"); 
     scanf("%lf", &itemPrice); 

     // I'm assuming you don't really want to add the -1 
     // entered for termination 
     if (itemPrice == -1) { 
      break; 
     } 

     if (i == *purchase_count) { 
      // array filled, let's get more space 
      // double it, or add a fixed amount, 
      // but rather not just one element each round 
      *purchase_count *= 2; 

      // we have the address saved in another variable, so here we can 
      // store the pointer returned by realloc in purchases without losing 
      // the handle if realloc fails 
      purchases = realloc(purchases, *purchase_count * sizeof *purchases); 

      if (purchases == NULL) { 
       // reallocation failed, now what? 

       // throw a tantrum? 
       free(*purchases_ptr); 
       exit(EXIT_FAILURE); 

       // or can something less drastic be done? 
      } else { 
       // Okay, got the needed space, let's record the address 
       *purchases_ptr = purchases; 
      } 
     } 
     purchases[i] = itemPrice; 
    } 

    // store the number of items actually read in? 
    *purchases_count = i; 

    // That should probably also get passed the number of items stored 
    displayCart(purchases); 

    *taxAmount = *subTotal * TAX_AMOUNT; 

    *totalPrice = *taxAmount + *subTotal; 

    printf("\nTotal comes to : $%.2lf\n", *totalPrice); 

    return *totalPrice; 
} 
+0

我真的很感激反饋意見,特別是在回購主要功能方面。我想我會需要一個二維指針,你會介意這是如何工作的? – theGrayFox

+0

我並不完全相信我明白你想要做的事情,但我會給它一個機會。將需要幾分鐘時間。 –

+0

不是問題,不急。我試圖將用戶需要的數量添加到數組中,而不是將其限制爲10個項目。 – theGrayFox