malloc 问
最近在 学 习malloc函数,哪位大虾有 malloc和 free模拟动 态存管理的 代码 啊 ,发出来 让 小弟学习学 习.最好 是 双链表 结构,顺便说 下 涵数接口 怎么 实现 的 ,万分 感谢!
[解决办法]
友情帮顶一下,最好找一些系统中的源码来看,毕竟很多系统里面的内存都是自己管理的,在下知道的数据库有BerkeleyDB,pg都是开源的里面有很多内存管理的源代码
[解决办法]
友情帮顶吧!
[解决办法]
没有代码1
[解决办法]
呵呵,自己写来玩玩的东西~~
通过动态内存分配实现c语言动态指针'数组'.
其实这种结构并非是一种数组,只是手动实现的一种类似数组的结构,实现类似数组的功能。应该可以说是一种伪数组结构吧。
#include <stdio.h>
#include <stdlib.h>
main()
{ char**p; int count,j,i; p=0;
scanf('%d',&count);
p=(char**)calloc(count,sizeof(char*));
for(j=0;j<count;j++)
p[j]=(char*)malloc(10*sizeof(char));
for(j=0;j<count;j++)
p[j]='ccccc';
for(i=0;i<count;i++)
{printf('%s\n',p[i]);
}
}
这种伪数组的两维都可以自定大小,只是各维的分配在内存不连续的区域。
[解决办法]
一个是用来申请内存的,一个是用来释放申请的内存的
这个在数据结构中经常使用的
[解决办法]
malloc动态的从内存中申请空间
free则将动态申请的空间释放,防止程序出现内存泄露等问题。
注意,free只能释放动态申请的内存空间,而不能释放静态的内存空间,像数组之类的。。
另外,内存管理之类的操作系统中有学过,可以去查阅一下。。
[解决办法]
循环链表的
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
#include<conio.h>
typedef struct
{
char cCargo[20];//货物
char cPrice[10];//价格
char cBarcode[15];//条形码
}Goods;//商品
typedef struct listnode
{
Goods data;
listnode *next;
}cyclelist;
void creatlist(cyclelist *head)
{
char ss[10],s[10]="退出";
cyclelist *p;
head->next=head;
printf("输入货物:退出 可停止录入信息\n");
printf("请输入货物信息:");
scanf("%s",ss);
while(strcmp(s,ss))
{
p=(cyclelist *)malloc(sizeof(cyclelist));
if(!p)
{
printf("没有分配到存储空间");
getch();
exit(0);
}
memset(p,0,sizeof(cyclelist));
strcpy(p->data.cCargo,ss);
printf("请输入价格信息:");
scanf("%s",p->data.cPrice);
printf("请输入条形码信息:");
scanf("%s",p->data.cBarcode);
p->next=head->next;
head->next=p;
printf("请输入商品信息:");
scanf("%s",ss);
}
}
void showlist(cyclelist *head)
{
cyclelist *p;
int sign=1;
p=head->next;
//while(p&&(p!=head)) 此处单独用p!=head 即可 书上的多余了
while(p!=head)
{
printf("商品:%s\n价格:%s\n条形码:%s\n",p->data.cCargo,p->data.cPrice,p->data.cPrice);
p=p->next;
sign=0;
}
if(sign)
{
printf("还没有输入任何信息\n");
getch();
}
}
void searchlist(cyclelist *head)
{
cyclelist *p;
p=head->next;
int sign,i=1;//标记
char s[15];
printf("请选择您准备输入的信息 1.商品 2.价格 3.条形码\n");
printf("您的选择为:");
scanf("%d",&sign);
switch(sign)
{
case 1:
{
printf("请输入您要查询的商品:");
scanf("%s",s);
while(p!=head)
{
if(!strcmp(s,p->data.cCargo))
{
printf("商品:%s\n价格:%s\n条形码:%s\n",p->data.cCargo,p->data.cPrice,p->data.cPrice);
i=0;
}
p=p->next;
}
if(i)
{
printf("没有搜索到相关的商品信息\n");
getch();
}
}
break;
case 2:
{
printf("请输入您要查询的价格:");
scanf("%s",s);
while(p!=head)
{
if(!strcmp(s,p->data.cPrice))
{
printf("商品:%s\n价格:%s\n条形码:%s\n",p->data.cCargo,p->data.cPrice,p->data.cPrice);
i=0;
}
p=p->next;
}
if(i)
{
printf("没有搜索到相关的价格信息\n");
getch();
}
}
break;
case 3:
{
printf("请输入您要查询的条形码:");
scanf("%s",s);
while(p!=head)
{
if(!strcmp(s,p->data.cBarcode))
{
printf("商品:%s\n价格:%s\n条形码:%s\n",p->data.cCargo,p->data.cPrice,p->data.cPrice);
i=0;
}
p=p->next;
}
if(i)
{
printf("没有搜索到相关条形码格信息\n");
getch();
}
}
break;
}
}
void dellist(cyclelist *head)
{
char s[10];
cyclelist *p=head->next,*q=head;
printf("请输入你想要删除的信息(商品):");
scanf("%s",s);
while(p!=head)
{
if(!strcmp(s,p->data.cCargo))
{
q->next=p->next;
free(p);
break;
}
else
{
q=p;
p=p->next;
}
}
}
void insertlist(cyclelist *head)
{
cyclelist *p,*q=head->next;
p=(cyclelist *)malloc(sizeof(cyclelist));
if(!p)
{
printf("没有分配到存储空间");
getch();
exit(0);
}
memset(p,0,sizeof(cyclelist));
while(q->next!=head)
{
q=q->next;
}
printf("请输入您想要插入的商品信息");
scanf("%s",p->data.cCargo);
printf("请输入您想要插入的价格信息");
scanf("%s",p->data.cPrice);
printf("请输入您想要插入的条形码信息");
scanf("%s",p->data.cBarcode);
p->next=q->next;
q->next=p;
}
void main ()
{
listnode a;
listnode *head=&a;
creatlist(head);
showlist(head);
searchlist(head);
dellist(head);
showlist(head);
insertlist(head);
showlist(head);
}
[解决办法]
自己乱写一气,尽量把系统搞崩溃。。。然后再去调试吧。
[解决办法]
C接口与实现中有讲,LZ可以找找,里面的代码实现的都很精妙
云风大牛很推崇这本书:
http://blog.codingnow.com/2009/11/array_c.html
[解决办法]
帮顶咯~~!
[解决办法]
http://www.codeproject.com/KB/cpp/Shared_Memory_Heap_Class.aspx
这个可以去研究一下