个人贷款闪光贷:malloc 在c++中通不过

来源:百度文库 编辑:杭州交通信息网 时间:2024/04/24 20:18:19
我在试着练习顺序表,可是总是在分配内存那里通不过,请大虾帮忙看一下错在哪里。谢谢了!
vc6

#include <malloc.h>
#include <stdlib.h>
#include <iostream>

# define true 1
# define false 0
# define OK 1
# define ERROR 0
# define INFEASIBLE -1
# define OVERFLOW -2

typedef int Status;
typedef float ElemType;

# define LIST_INIT_SIZE 100; //线形表存储空间初始分配量
# define LISTINCREMENT 10; //线形表存储空间的分配增量

typedef struct{
ElemType *elem; //存储空间地址
int length; //当前长度
int listsize; //当前分配存储容量(sizeof(ElemType)为单位)
}Sqlist;

// InitList_sq

Status InitList_Sq(Sqlist &L){
L.elem=(ElemType *) malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!L.elem)exit(OVERFLOW);
L.length=0;
L.listsize=LIST_INIT_SIZE;
return OK;
}

//销毁顺序表
Status Destroy_Sq(Sqlist &L){
free(L.elem);
return OK;
}

//清空顺序表
Status Clear_Sq(Sqlist &L){
L.length=0;
return OK;
}

//判断是否为空表
Status Empty_Sq(Sqlist &L){
if (L.length==0)
return true;
else
return false;
}

//返回顺序表元素的个数
Status ListLength_Sq(Sqlist &L){
return L.length;
}

//取顺序表中第i个元素,并返回
Status GetElem_Sq(Sqlist L,int i,float &e){
if(1<=i&&i<=L.length)
e=*(L.elem+i);
else
return ERROR;
}

//插入数据元素
Status ListInsert_Sq(Sqlist &L,int i,ElemType e ){
ElemType *newbase;
ElemType *p,*q;
if(1>i||i>L.length+1)return ERROR;
if(L.length>=L.listsize){
newbase=(ElemType *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
if(!newbase)exit(OVERFLOW);
L.elem=newbase;
L.listsize+=LISTINCREMENT;
}
q=(L.elem+i-1);
for(p=(L.elem+L.length-1);p>=q;--p)*(p+1)=*p;
*q=e;
++L.length;
return OK;
}

//顺序表的删除

Status ListDelete_Sq(int i,Sqlist &L, ElemType &e){
ElemType *p,*q;
if(i<1||i>L.length)return ERROR;
e=*(L.elem+i-1);
for( ;i<L.length;i++)
*(L.elem+i-1)=*(L.elem+i);
L.length-=L.length;
return OK;
}

int main()
{

Sqlist my_flist;
InitList_Sq(my_flist);

return OK;
}

c++里面使用运算符new()与运算符delete()能更好地进行 内存的分配和释放,运算符new的使用形式为:
p=new type; //type为所需的数据类型
//p为指向该类型的指针
系统会从自由存储区中为程序分配一块sizeof(type)字节大小的内存,使用后再用 delete 释放就可以了:
delete p;