C 已分配动态数组,但无法使用它
我希望将此代码用作动态数组。不幸的是,我无法理解为什么程序不使用分配的内存。C 已分配动态数组,但无法使用它,c,arrays,dynamic,C,Arrays,Dynamic,我希望将此代码用作动态数组。不幸的是,我无法理解为什么程序不使用分配的内存。AddToArray函数的参数可能有问题吗 可以将此代码直接复制并粘贴到IDE中并进行编译,以查看输出。内存似乎已分配但未使用 #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct { float x; float y; } DATA; int AddToArray (DA
AddToArray
函数的参数可能有问题吗
可以将此代码直接复制并粘贴到IDE中并进行编译,以查看输出。内存似乎已分配但未使用
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
float x;
float y;
} DATA;
int AddToArray (DATA item,
DATA **the_array,
int *num_elements,
int *num_allocated)
{
if(*num_elements == *num_allocated)
{ // Are more refs required?
// Feel free to change the initial number of refs
// and the rate at which refs are allocated.
if (*num_allocated == 0)
{
*num_allocated = 3; // Start off with 3 refs
}
else
{
*num_allocated *= 2;
}// Double the number of refs allocated
// Make the reallocation transactional by using a temporary variable first
void *_tmp = realloc(*the_array, (*num_allocated * sizeof(DATA)));
// If the reallocation didn't go so well, inform the user and bail out
if (!_tmp)
{
printf("ERROR: Couldn't realloc memory!\n");
return(-1);
}
// Things are looking good so far, so let's set the
*the_array = (DATA*)_tmp;
}
(*the_array)[*num_elements] = item;
*num_elements++;
return *num_elements;
}
int main()
{
DATA *the_array = NULL;
int num_elements = 0; // To keep track of the number of elements used
int num_allocated = 0; // This is essentially how large the array is
// Some data that we can play with
float numbers1[4] = {124.3,23423.4, 23.4, 5.3};
float numbers2[4] = { 42, 33, 15, 74 };
int i;
// Populate!
for (i = 0; i < 4; i++)
{
DATA temp;
temp.x = numbers1[i];
temp.y = numbers2[i];
if (AddToArray(temp, &the_array, &num_elements, &num_allocated) == -1)
return 1; // we'll want to bail out of the program.
}
for (i = 0; i < 4; i++)
{
printf("(x:%f,y:%f)\n", the_array[i].x, the_array[i].y);
}
printf("\n%d allocated, %d used\n", num_allocated, num_elements);
// Deallocate!
free(the_array);
// All done.
return 0;
}
#包括
#包括
#包括
类型定义结构{
浮动x;
浮动y;
}数据;
int AddToArray(数据项,
数据**u数组,
int*num_元素,
int*num_(已分配)
{
如果(*num_元素==*num_已分配)
{//是否需要更多参考文献?
//可以随意更改引用的初始数量
//以及分配引用的速率。
如果(*分配的数量==0)
{
*num_allocated=3;//从3个参考开始
}
其他的
{
*分配的数量*=2;
}//将分配的引用数增加一倍
//首先使用临时变量使重新分配成为事务性的
void*_tmp=realloc(*数组,(*num_分配*sizeof(数据));
//如果重新分配不顺利,通知用户并退出
如果(!\u tmp)
{
printf(“错误:无法重新分配内存!\n”);
返回(-1);
}
//到目前为止,情况看起来不错,所以让我们设定目标
*_数组=(数据*)_tmp;
}
(*数组)[*num\u元素]=项;
*num_元素++;
返回*num_元素;
}
int main()
{
DATA*_数组=NULL;
int num_elements=0;//跟踪使用的元素数
int num_allocated=0;//这实际上就是数组的大小
//一些我们可以利用的数据
浮点数1[4]={124.323423.4,23.4,5.3};
浮点数2[4]={42,33,15,74};
int i;
//填充!
对于(i=0;i<4;i++)
{
数据温度;
温度x=数字1[i];
温度y=数字2[i];
if(AddToArray(temp、&u数组、&num\u元素、&num\u分配)=-1)
return 1;//我们希望退出该程序。
}
对于(i=0;i<4;i++)
{
printf(((x:%f,y:%f)\n),数组[i].x,数组[i].y);
}
printf(“\n分配了%d,使用了%d”,分配了num\u,元素num\u);
//取消分配!
free(_数组);
//都做完了。
返回0;
}
在您的代码中,您需要更改
*num_elements++代码>
到
(*num_元素)+代码>
因为在没有显式括号的情况下,++
的优先级高于*
运算符。您想要的是增加存储在地址中的值,而不是相反
检查运算符优先级。在代码中,您需要更改
*num_elements++代码>
到
(*num_元素)+代码>
因为在没有显式括号的情况下,++
的优先级高于*
运算符。您想要的是增加存储在地址中的值,而不是相反
检查运算符优先级。试试看
(*num_elements)++;
在功能上
AddToArray()
试一试
在功能上
AddToArray()
代码中的错误在于指针是递增的,而不是值
..
(*the_array)[*num_elements] = item;
(*num_elements)++;
..
这是一种编程作业吗?代码可以在许多方面得到改进。
对于这类问题,有很多好的算法都编写得很好,并且经过优化,我建议您在这方面进行一些研究。代码中的错误在于指针是递增的,而不是值
..
(*the_array)[*num_elements] = item;
(*num_elements)++;
..
这是一种编程作业吗?代码可以在许多方面得到改进。
对于这类问题,有很多好的算法都写得很好,并且经过优化,我建议您在这方面进行一些研究。为什么需要从第三个参考或第二个参考开始?为什么需要从第三个参考或第二个参考开始。。。?