找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 3113|回复: 0

ACE内存管理

[复制链接]
发表于 2008-10-25 20:48:10 | 显示全部楼层 |阅读模式
刚接触ace,看“ace程序员教程”,中第三章关于ACE_Cached_Allocator的例子,自己试着在linux下编译了下,出错,哪位达人帮忙看了解决下
例子:
#include "ace/Malloc.h"
#include "ace/OS.h"

typedef char MEMORY_BLOCK[1024];
typedef ACE_Cached_Allocator<MEMORY_BLOCK,ACE_SYNCH_MUTEX> Allocator;
class MessageManager
{
public:
MessageManager(int n_blocks):
    allocator_(n_blocks),message_count_(0)
{
    mesg_array_=new char*[n_blocks];
}

void allocate_msg(const char *msg)
{
    mesg_array_[message_count_]=allocator_.malloc(ACE_OS::strlen(msg)+1);
    ACE_OS::strcpy(mesg_array_[message_count_],msg);
    message_count_++;
}
void free_all_msg()
{
    for(int i=0;i<message_count_;i++)
        allocator_.free(mesg_array_);
   message_count_=0;
}
void display_all_msg()
{
    for(int i=0;i<message_count_;i++)
        ACE_OS::printf("%s\n",mesg_array_);
}
private:
char **mesg_array_;
Allocator allocator_;
int message_count_;
};

int main(int argc, char* argv[])
{
if(argc<2)
{
    ACE_DEBUG((LM_DEBUG, "Usage: %s <Number of blocks>\n", argv[0]));
    exit(1);
}
int n_blocks=ACE_OS::atoi(argv[1]);

MessageManager mm(n_blocks);

while(1)
{

    ACE_DEBUG((LM_DEBUG,"\n\n\nAllocating Messages\n"));
    for(int i=0; i<n_blocks;i++){
        ACE_OS::sprintf(message,"Message %d: Hi There",i);
        mm.allocate_msg(message);
    }
        ACE_DEBUG((LM_DEBUG,"Displaying the messages\n"));
    ACE_OS::sleep(2);
    mm.display_all_msg();

    ACE_DEBUG((LM_DEBUG,"Releasing Messages\n"));
    ACE_OS::sleep(2);
    mm.free_all_msg();
}

return 0;
}

编译:g++ -o message -I$ACE_ROOT -L$ACE_ROOT/ace -lACE Message.cpp

Message.cpp:5: error: expected initializer before ‘<’ token
Message.cpp:34: error: ‘Allocator’ does not name a type
您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-11-23 00:05 , Processed in 0.016541 second(s), 7 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

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