天嵌 ARM开发社区

 找回密码
 注册
查看: 2732|回复: 2

realloc(): invalid next size: 0x09ce4008 *** 问题

[复制链接]
raceant 发表于 2011-9-28 18:02:27 | 显示全部楼层 |阅读模式
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <malloc.h>

  4. #define Maxsize 10

  5. typedef int Elemtype;        //将int定义为Elemtype
  6. typedef struct{
  7.         Elemtype *elem;
  8.         int length;
  9.         int listsize;
  10. } sqlist;
  11. //初始化一个顺序表
  12. //参数L:sqlist类型的指针
  13. void initSqlist(sqlist *L)
  14. {
  15.         L->elem=(int *)malloc(Maxsize*sizeof(Elemtype));
  16.         if(!L->elem) exit(0);
  17.         L->length=0;
  18.         L->listsize=Maxsize;
  19. }
  20. void insertElem(sqlist *L,int i,Elemtype item)   //向第i个位置上插入item
  21. {
  22.         //Elemtype *base,*insertPtr,*p;
  23.         Elemtype *insertPtr,*p;
  24.         Elemtype *base;
  25.         if(i<1||i>L->length+1) exit(0);       
  26.         if(L->length>=L->listsize)
  27.         {
  28.         [color=Red]        base=(Elemtype*)realloc(L->elem,(L->listsize+10)*sizeof(Elemtype));[/color]
  29.                 if(!base) exit(0);
  30.                 L->elem=base;
  31. //                L->elem=(int *)realloc(L->elem,20*sizeof(int));
  32.                 L->listsize=L->listsize+10;
  33.         }
  34.         insertPtr=&(L->elem[i-1]);
  35.         for(p=&(L->elem[L->length+1]);p>insertPtr;p--)
  36.                 *(p+1)=*p;
  37.         *insertPtr=item;
  38.         L->length++;
  39. }
  40. void delElem(sqlist *L,int i)   //删除第i个元素
  41. {
  42.         Elemtype *delItem,*q;
  43.         if(i<1||i>L->length)  exit(0);
  44.         delItem=&(L->elem[i-1]);
  45.         q=L->elem+L->length-1;
  46.         for(++delItem;delItem<=q;++delItem)
  47.                 *(delItem-1)=* delItem;
  48.         L->length--;       
  49. }

  50. void main()
  51. {
  52.         sqlist l;
  53.         int i;
  54.         initSqlist(&l);
  55.         printf("biaozhi1\n");
  56.         for(i=0;i<15;i++)
  57. {                insertElem(&l,i+1,i+1);
  58.         printf("biaozhi2\n");
  59. }               
  60.         printf("\nThe content of the list is \n");
  61.         for(i=0;i<l.length;i++)
  62.                 printf("%d",l.elem[i]);
  63.         delElem(&l,5);
  64.         printf("\nDelete the fifth element\n");
  65.         for(i=0;i<l.length;i++)
  66.                 printf("%d",l.elem[i]);
  67.                 printf("\n");
  68. }
复制代码
这个程序在VC上可以通过 但gcc却通不过  
找了好多资料都说是指针没初始化 我觉得我的没这个问题啊 问题锁定再realloc这个函数上
 楼主| raceant 发表于 2011-10-8 21:29:06 | 显示全部楼层
唉  问题依旧没解决,自己顶下
TQ-lkp 发表于 2011-10-18 17:10:41 | 显示全部楼层
base=(Elemtype*)realloc(L->elem,(L->listsize+10)*sizeof(Elemtype));
这个地方追加分配的内在数量应该为L->listsize+10*4吧,你下面只加了10个
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

i.MX8系列ARM cortex A53 M4 工控板上一条 /1 下一条

Archiver|手机版|小黑屋|天嵌 嵌入式开发社区 ( 粤ICP备11094220号-2 )

GMT+8, 2024-10-1 17:24 , Processed in 1.028255 second(s), 18 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表