user-0 提问: 练习线性表时realloc出错 Error in `./a.out': realloc(): invalid next size: 0x00000000

回复:
user: user-1
content: 刚测试，发现问题是在输入num为大于100，比如134时会出现问题。其实问题主要是因为内存越界，而不是realloc导致的，数组越界导致realloc出错。  可以看一下你的main函数里，先初始化了队列elem，但是申请的长度是100，但是如果num大于100，那么如下的赋值已经越界了。  scanf("%d",&num);     for(i=0;i<num;i++)     {         list.elem[i]=i;         list.length++;     } 这个地方越界，就会导致realloc出错。  解决办法时，若num的输入大于100，那么只对100以内的空间赋值，比如： for(i=0;i < ((num > 100)?100:num);i++)     {         list.elem[i]=i;         list.length++;     }  供参考！

user: user-2
content: #include<stdio.h> #include<stdlib.h> #include<string.h> //#ifndef SEQUENCE_LINER_LIST_H //#define SEQUENCE_LINER_LIST_H  /*线性表的顺序实现*/  #define LIST_INIT_SIZE 100 //初始化分配量 #define LIST_INCREMENT 10 //增量 #define LIST_TYPE int //存储类型  typedef struct strSequenceLinerList {     LIST_TYPE *elem; //存储空间基址     unsigned int length; //当前长度     unsigned int listSize; //当前存储容量 }SequenceLinerList; typedef enum _status {     OK,     FAILED,     OVERFLOW }Status;  /*线性表的初始话*/  Status initSequenceLinerList(SequenceLinerList *list) {     list->elem = (LIST_TYPE *)malloc(sizeof(LIST_TYPE)*LIST_INIT_SIZE);     if(!list->elem)     {         return OVERFLOW;     }     memset(list->elem,0,sizeof(LIST_TYPE)*LIST_INIT_SIZE);     list->length = 0;     list->listSize = LIST_INIT_SIZE;     return OK; }  /*扩展*/  Status resizeSequenceLinerList(SequenceLinerList *list) {     printf("Resize...list->elem:%p,sizeof(LIST_TYPE):%d *((list->listSize:%d)+LIST_INCREMENT:%d):%d\n",         list->elem,         sizeof(LIST_TYPE),         list->listSize,         LIST_INCREMENT,         sizeof(LIST_TYPE)*((list->listSize)+LIST_INCREMENT)     );    LIST_TYPE *newbee = (LIST_TYPE*)realloc(list->elem,sizeof(LIST_TYPE)*((list->listSize)+LIST_INCREMENT));     if(!newbee){         return OVERFLOW;     }     list->elem = newbee;     list->listSize += LIST_INCREMENT;     return OK; }  /*打印*/  Status printSequenceLinerList(SequenceLinerList *list) {     printf("Begin printf list...\n");     printf("length = %d,listSize = %d\n",list->length,list->listSize);     if(list->length == 0 || list->elem == NULL){         printf("SequenceLinerList is NULL\n");         return FAILED;     }     else     {         unsigned int i=0;         for(;i<list->length-1;++i)         {             printf("%d,",list->elem[i]);         }         printf("%d\n",list->elem[i]);         printf("End print list\n");         return OK;     } } /*线性表插入：position表示插入的位置从1开始 第一个元素是list->elem[0]*/  Status insertSequenceLinerList(SequenceLinerList *list,unsigned int position,unsigned int value) {     if(position < 1 ||position > (list->length + 1))     {         printf("insert error\n");         return FAILED;     }     if(list->length > list->listSize-1)     {         Status res = resizeSequenceLinerList(list);         if(OK != res)         {             return FAILED;         }     }     LIST_TYPE *q = &(list->elem[position - 1]);     LIST_TYPE *p = &(list->elem[list->length - 1]);     for(; p >= q;--p)     {         *(p + 1)=*p;     }     *q = value;     ++list->length;     return OK; }  /*删除元素*/  Status deleteSequenceLinerList(SequenceLinerList *list,unsigned int position) {     if(position < 1 || position > list->length)     {         return FAILED;     }     LIST_TYPE* p = &list->elem[position - 1];     LIST_TYPE* q = &list->elem[list->length];     for(;p != q;++p)     {         *p = *(p + 1);     }     --list->length;     return OK; }  /* 查找一个值的下标*/  unsigned int findSequenceLinerList(SequenceLinerList *list,int value) {     unsigned int i=0;     for(;i<list->length;i++)     {         if(value == list->elem[i])         {             return i;         }     }     return -1; }  /*排序(冒泡)*/  Status sortSequenceLinerList(SequenceLinerList *list) {     unsigned int i=0;     unsigned int j=0;     for(;i<list->length - 1;++i)     {         for(j=i+1;j<list->length;++j)         {             if(list->elem[i] > list->elem[j])             {                 LIST_TYPE tmp = list->elem[i];                 list->elem[i] = list->elem[j];                 list->elem[j] = tmp;             }         }     }     return OK; }  /*销毁*/  Status destroySequenceLinerList(SequenceLinerList *list) {     free(list->elem);     list->elem = NULL;     return OK; } //#endif int main(void){     SequenceLinerList list;     initSequenceLinerList(&list);     int i;     int num;     scanf("%d",&num);     for(i=0;i<num;i++)     {         insertSequenceLinerList(&list,i+1,i+1);     }     printSequenceLinerList(&list);     return 0; }  // 132 // Resize...list->elem:00382088,sizeof(LIST_TYPE):4 *((list->listSize:100)+LIST_INCREMENT:10):440 // Resize...list->elem:00383288,sizeof(LIST_TYPE):4 *((list->listSize:110)+LIST_INCREMENT:10):480 // Resize...list->elem:00383288,sizeof(LIST_TYPE):4 *((list->listSize:120)+LIST_INCREMENT:10):520 // Resize...list->elem:00383288,sizeof(LIST_TYPE):4 *((list->listSize:130)+LIST_INCREMENT:10):560 // Begin printf list... // length = 132,listSize = 140 // 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30 // ,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57 // ,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83 // ,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107 // ,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127 // ,128,129,130,131,132 // End print list

user: user-2
content: /*扩展*/  Status resizeSequenceLinerList(SequenceLinerList *list) { 	printf("Resize...list->elem:%p,sizeof(LIST_TYPE):%d *((list->listSize:%d)+LIST_INCREMENT:%d):%d\n",         list->elem,         sizeof(LIST_TYPE),         list->listSize,         LIST_INCREMENT,         sizeof(LIST_TYPE)*((list->listSize)+LIST_INCREMENT) 	); 	LIST_TYPE *newbee =	(LIST_TYPE*)realloc(list->elem,sizeof(LIST_TYPE)*((list->listSize)+LIST_INCREMENT)); 	if(!newbee){ 		return OVERFLOW; 	} 	list->elem = newbee; 	list->listSize += LIST_INCREMENT; 	return OK; }  运行观察一下。