参考:http://blog.csdn.net/feixiaoxing/article/details/6847808 作者:feixiaoxing
本程序可作为内存分配和管理练习, 熟悉这个程序并能自己写出代码,说明对c++中内存分配和管理有了一定的了解。
程序说明:
采用一个结构体,保存了数据的个数和使用情况(用一个字节 char类型,字节中八个位若为1说明这位已经被分配
若为0则说明未分配 )。
说明: 如果要申请10个数据存储空间,则需要两个char(2*8=16)来表示,其中有6位没有用到。若要申请24位存储空间,则需要三个char来表示,每一个char数据表示八个空间是否被占用。
代码可在csdn 免积分下载:http://download.csdn.net/detail/lmnxjf/5329998
下面是代码:
#include<iostream>
#include<assert.h>
using namespace std;
struct DataNode
{
int* p_data;
char* p_flag;
int num;
};
// 创建节点
DataNode* NewNode(int num)
{
if(0== num)
return NULL;
DataNode* temp_node = new DataNode;
assert(temp_node !=NULL);
memset(temp_node, 0, sizeof(temp_node));
temp_node->p_data = new int[num];
assert(temp_node->p_data !=NULL);
temp_node->p_flag =new char[ (num +7)>>3];
if(temp_node->p_flag == NULL)
{
delete []temp_node->p_data;
delete temp_node;
return NULL;
}
temp_node->num = num;
memset( temp_node->p_data, 0 , sizeof(int)*num);
memset( temp_node->p_flag, 0 , sizeof(char)*((num+7)>>3));
return temp_node;
}
//删除节点
void DeleteNode(const DataNode* node)
{
if(node==NULL)
return ;
delete []node->p_data;
delete []node->p_flag;
delete node;
}
// 删除数据
bool DeleteNodeData(const DataNode* node,const int* node_data)
{
if(NULL== node || NULL==node_data)
return false;
if(node_data< node->p_data || node_data> (node->p_data+ node->num))
return false;
int pos = (node_data - node->p_data) ;/// sizeof(int);
node->p_flag[pos>>3] &= (~(0x01<<(pos%8) ) );
return true;
}
//找到可以分配内存的位置
int FreeDataLocation(const DataNode* node)
{
int num= node->num;// 最多可分配内存的大小
int location=0; //可以内存位置
//该位若为1说明这位已经被分配
// 若为0则说明可以分配 由char来表示8个数据类型
while(location<= num)
{
if( node->p_flag[location>>3] & (0x01<<(location%8)))
++location;
else
return location;
}
return -1;
}
// 分配内存
int* IsertDataToNode(const DataNode* node)
{
if(node==NULL)
{
cout<<"can't insert data"<<endl;
return NULL;
}
int pos=0;
if(-1 ==(pos= FreeDataLocation(node)))
{
cout<<"Memory is alread full "<<endl;
return NULL;
}
else
{
node->p_flag[pos>>3] |= (0x01<<(pos%8));
return node->p_data+pos;
}
}
//剩余内存的个数
int CountOfFree(const DataNode* node)
{
int counts=0;
while(counts<= node->num)
{
if(node->p_flag[counts>>3] &(0x01<< (counts%8)))
++counts;
else
return node->num - counts;
}
return node->num - counts;
}
void DisplayNodeData(const DataNode* node)
{
int counts=0;
if(node== NULL)
{
cout<<"there is no data!"<<endl;
return;
}
else
{
while(node->p_flag[counts>>3] & (0x01<<(counts%8)))
{
cout<<"data:"<<*(node->p_data+counts)<<endl;
++counts;
}
cout<<"express with Binary:";
for(int i=0; i< node->num ; i++)
{
//cout<<hex<<node->p_flag[counts]<<" ";
if(node->p_flag[i>>3] & (0x01<<(i%8) ))
cout<<"1";
else
cout<<"0";
}
cout<<endl;
}
}
实现文件:
int main()
{
cout<<"----------------------------------------------------"<<endl;
cout<<"\t\t\t\t声明"<<endl;
cout<<"\t 利用char八位来表示,数据存储情况"<<endl;
cout<<"\t 1表示已经占用, 0 表示未被占用"<<endl;
cout<<" 一路向南\n";
cout<<" 2013,5,4"<<endl;
cout<<"----------------------------------------------------"<<endl;
DataNode* data_node = NewNode(10);
cout<<"counst of free:"<<CountOfFree(data_node)<<endl<<endl;
int* data=NULL;
data=IsertDataToNode(data_node);
*data =5;
data=IsertDataToNode(data_node);
*data =6;
data=IsertDataToNode(data_node);
*data =12;
data=IsertDataToNode(data_node);
*data =13;
data=IsertDataToNode(data_node);
*data =15;
cout<<"----------------------Display--------------------------------"<<endl;
DisplayNodeData(data_node);
cout<<"\nafter insert data counst of free:"<<CountOfFree(data_node)<<endl;
cout<<"----------------------Display--------------------------------"<<endl;
cout<<endl<<"after delete:";
DeleteNodeData(data_node, data);
DisplayNodeData(data_node);
cout<<"\nafter delete data counst of free:";
cout<<CountOfFree(data_node)<<endl;
}
运行结果:
自己写的小代码, 若发现有什么问题, 谢谢指正。 相互学习