2012-12-27 55 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