当前位置:首页 > 代码 > 正文

malloc源代码(mallocc)

admin 发布:2022-12-19 15:26 136


今天给各位分享malloc源代码的知识,其中也会对mallocc进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

C语言代码求解 源代码如下,求改,求解答

1、目测p1没用,建议去掉;

2、scanf(“%f”,p++);改为scanf(“%f”,p+i);;

3、去掉p=p1;这句;

你改完再试一试。

C语言malloc函数出错

readyqueue-front=(linknode *)malloc(size)一定出错!

因为,此时readyqueue=NULL,它没办法指定-front成员并进行操作.

首先,要给readyqueue赋值或申请空间,前面加上:

readyqueue=(linkqueue*)malloc(sizeof(linkqueue));应该就行了.

急!急!建立一个动态链表,用malloc函数开辟新结点,结点包含姓名、年龄、学号,然后输出的源代码,急求!

#includestdio.h

#includemalloc.h

#includestring.h#define NAMEMAX 50

#define NOMAX 20

#define DATATYPE STUDENT

typedef struct node

{

void *data;

struct node *left;

struct node *right;

}NODE;typedef struct

{

struct node *head;

struct node *current;

struct node *rear;

int count;

}linklist;typedef struct

{

char name[NAMEMAX];

int age;

char no[NAMEMAX];

}DATATYPE;void init_linklist(linklist *list)

{

list-head=NULL;

list-current=NULL;

list-rear=NULL;

list-count=0;

}void add_linklist(linklist *list,void *data)

{

NODE *newNode;

newNode=(NODE *)malloc(sizeof(NODE));

newNode-data=data;

if(list-head==NULL)

{

list-head=newNode;

list-current=newNode;

list-rear=newNode;

newNode-left=NULL;

newNode-right=NULL;

list-count++;

}

else

{

list-rear-right=newNode;

newNode-left=list-rear;

newNode-right=NULL;

list-rear=newNode;

list-current=newNode;

list-count++;

}

}

void free_list(linklist *list,DATATYPE*)

{

NODE *tmp=list-head;

while(tmp!=NULL)

{

if(tmp-left==NULL)

{

tmp=tmp-right;

continue;

}

else if(tmp-right==NULL)

{

free((DATATYPE*)(tmp-data));

free(tmp);

break;

}

else

{

free((DATATYPE*)(tmp-left-data));

free(tmp-left);

tmp=tmp-right;

}

} init_linklist(list);

return;

}void printlist(linklist *list,DATATYPE*)

{

NODE *tmp=list-head;

int i=0;

while(tmp!=NULL)

{

printf("第%d条记录:\n",++i);

printf("----------------------------\n");

printf("姓名为:%s\n",((DATATYPE*)(tmp-data))-name);

printf("年龄为:%d\n",((DATATYPE*)(tmp-data))-age);

printf("学号为:%s\n",((DATATYPE*)(tmp-data))-no);

printf("----------------------------\n");

tmp=tmp-right;

}

return;

}int main()

{

DATATYPE *pStu=NULL;

char buf[50]={0};

linklist list;

init_linklist(list);

while(1)

{

pStu=(DATATYPE*)malloc(sizeof(DATATYPE));

memset(pStu,0,sizeof(DATATYPE)); printf("请输入姓名:");

fgets(pStu-name,NAMEMAX,stdin);

pStu-name[strlen(pStu-name)-1]=0; printf("请输入年龄:");

scanf("%d",(pStu-age));

getchar(); printf("请输入序号:");

fgets(pStu-no,NOMAX,stdin);

pStu-no[strlen(pStu-no)-1]=0; add_linklist(list,pStu);

printf("是否继续添加(y/n):");

memset(buf,0,50);

fgets(buf,50,stdin);

buf[strlen(buf)-1]=0;

if (!strcmp("y",buf) || !strcmp("Y",buf))

continue;

else

break;

}

printf("\n结果为:\n");

printlist(list,0);

free_list(list,0);

return 0;

} //void printlist(linklist *list,DATATYPE*) 楼下的这DATATYPE后面这个类型没意思?? 告诉你这是显示强调里面要使用DATATYPE这个类型,而又不使用它 printlist(list,0); 这句就能体现,还有个功能就是释放内存时你考虑了没 虽然这题free void*指针是可以释放的 但是如果在C++ 中你如果直接释放void*指针 而不指定其类型 那么这个对象就释放不测底 。#define DATATYPE STUDENT 我定义这个宏的目的就是能够方便的智能的释放内存。 如果你把STUDENT的这个结构体改了 不叫这个名字 那么岂不是又要在相关的代码更改名字,而我这个只要修改这个宏就行了,我写这么复杂?告诉你我这个代码基本是直接拷贝的 这代码是以前写过的,使用的通用双向链表 我只是截取了我写的一部分函数写代码的目的是要通用,要能够重复利用。

malloc源代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于mallocc、malloc源代码的信息别忘了在本站进行查找喔。

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://ahzz.com.cn/post/10300.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载