我最初拥有数量为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;
}
答案 0 :(得分:1)
double* purchases = (double*)malloc(sizeof(double)*QUANTITY_SIZE);
更重要的是:
storeMoreItems = (double*)realloc(storeMoreItems, i * sizeof(double));
您尝试分配i*sizeof(int)
,然后将其投放到double*
。当double
和int
具有不同的大小时,您的代码将很难找到错误。
接下来的事情:
当i
等于0时,您分配内存,初始大小为0字节(i*sizeof(int)
),然后尝试使用它。它不会起作用。尝试以这种方式更改循环:for (int i = 1, i <= QUANTITY_SIZE;...
并保留purchases[i-1]
。
答案 1 :(得分:1)
这是错误的:
if(storeMoreItems != NULL)
{
storeMoreItems = purchases;
purchases[i-1] = itemPrice;
}
首先,你覆盖刚刚realloc
ed指针,你想要
purchases = storeMoreItems;
而不是反过来。但这不会影响传入的purchases
指针在调用函数中的值。
为此,您需要将purchases
的地址从main
传递到
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;
}
答案 2 :(得分:0)
您要做的第一件事是确保分配正确的字节数。 Malloc不知道您想要将内存用于双打,因此您需要乘以sizeof(double)
:
double *purchases = (double*)malloc(QUANTITY_SIZE * sizeof(double));
答案 3 :(得分:0)
查看realloc。
void * realloc(void * ptr,size_t size);
重新分配内存块更改ptr。
指向的内存块的大小从此处获取更多详情http://www.cplusplus.com/reference/cstdlib/realloc/