在动态数组实现中使用 realloc()
我正在尝试使用 realloc() 在 C 中实现动态数组。我的理解是 realloc() 保留旧指针指向的内存块的旧内容,但我的以下测试代码表明情况并非如此:
#include <stdio.h>
#include <stdlib.h>
int DARRAYSIZE=5;
typedef struct dynamicArray{
int size;
int *items;
}DArray;
int init(DArray *DAP){ //initialise the DArray
DAP->items=(int *)malloc(sizeof(int)*DARRAYSIZE);
if(DAP->items){
DAP->size=0;
return 0;
}else{
printf("Malloc failed!\n");
return 1;
}
}
void print(DArray *DAP){ //print all elements in the DArray
int i=0;
for(;i<DAP->size;i++)
printf("%d\t",DAP->items[i]);
printf("\n");
}
void add(DArray *DAP,int val){ //add the new val into the DArray
if(!full(DAP)){
DAP->items[DAP->size++]=val;
}else{
if(!grow(DAP)){
DAP->items[DAP->size++]=val;
}else
exit(1);
}
}
int full(DArray *DAP){ //returns 1 if the DAarray is full
if(DAP->size==DARRAYSIZE)
return 1;
else
return 0;
}
int grow(DArray *DAP){ //grows the DArray to double its original size
int *temp=(int *)realloc(DAP->items,DARRAYSIZE*2);
if(!temp){
printf("Realloc failed!\n");
return 1;
}else{
DAP->items=temp;
DARRAYSIZE*=2;
printf("Darray doubled and current contents are:\n");
print(DAP);
return 0;
}
}
void destroy(DArray *DAP){ //destroies the DArray
free(DAP->items);
}
int main(void){
DArray newDA;
if(!init(&newDA)){
int i;
for(i=1;i<30;i++)
add(&newDA,i);
}else
exit(1);
destroy(&newDA);
return 0;
}
我所做的是在函数 Growth() 中数组大小加倍后立即打印数组的内容。 编译了代码
我使用: :gcc -version i686-apple-darwin11-llvm-gcc-4.2
及以下是输出:
中包含意外的 0输出。
请告诉我我在这里做错了什么,谢谢!
I'm trying to implement dynamic array in C using realloc(). My understanding is that realloc() preserves old contents of the memory block the old pointer points to, but my following testing code suggests otherwise:
#include <stdio.h>
#include <stdlib.h>
int DARRAYSIZE=5;
typedef struct dynamicArray{
int size;
int *items;
}DArray;
int init(DArray *DAP){ //initialise the DArray
DAP->items=(int *)malloc(sizeof(int)*DARRAYSIZE);
if(DAP->items){
DAP->size=0;
return 0;
}else{
printf("Malloc failed!\n");
return 1;
}
}
void print(DArray *DAP){ //print all elements in the DArray
int i=0;
for(;i<DAP->size;i++)
printf("%d\t",DAP->items[i]);
printf("\n");
}
void add(DArray *DAP,int val){ //add the new val into the DArray
if(!full(DAP)){
DAP->items[DAP->size++]=val;
}else{
if(!grow(DAP)){
DAP->items[DAP->size++]=val;
}else
exit(1);
}
}
int full(DArray *DAP){ //returns 1 if the DAarray is full
if(DAP->size==DARRAYSIZE)
return 1;
else
return 0;
}
int grow(DArray *DAP){ //grows the DArray to double its original size
int *temp=(int *)realloc(DAP->items,DARRAYSIZE*2);
if(!temp){
printf("Realloc failed!\n");
return 1;
}else{
DAP->items=temp;
DARRAYSIZE*=2;
printf("Darray doubled and current contents are:\n");
print(DAP);
return 0;
}
}
void destroy(DArray *DAP){ //destroies the DArray
free(DAP->items);
}
int main(void){
DArray newDA;
if(!init(&newDA)){
int i;
for(i=1;i<30;i++)
add(&newDA,i);
}else
exit(1);
destroy(&newDA);
return 0;
}
What I did was print the contents of the array as soon as its size is doubled in function grow(). I compiled the code using:
:gcc -version
i686-apple-darwin11-llvm-gcc-4.2
and below is the output:
with the unexpected 0's in the output.
Please kindly advise what I'm doing wrong here, thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在
realloc()
中忘记了sizeof(int)
,因此不断缩小数组。您还需要跟踪动态数组结构中使用的项数和分配的空间量;这是两项单独的措施,而且都是必要的。但是您不能使用全局变量(当前为 DYNARRAYSIZE)来保存每个动态数组的大小。每个动态数组都需要一个。
您还需要查看full()
;它将大小与 DARRAYSIZE 进行比较...总是!工作输出
使用制表位设置为 3 进行格式化
工作代码
You forgot
sizeof(int)
in yourrealloc()
, so you keep shrinking your array.You also need to keep track of the number of items in use and the amount of space allocated in the dynamic array structure; these are two separate measures, and both are needed. But you can't use a global variable (currently DYNARRAYSIZE) to hold the size of every dynamic array. You need one per dynamic array.
You also need to look atfull()
; it compares the size with DARRAYSIZE...always!Working Output
Formatted with tabstops set at 3
Working Code