liubofxc 发表于 2008-10-25 20:48:10

ACE内存管理

刚接触ace,看“ace程序员教程”,中第三章关于ACE_Cached_Allocator的例子,自己试着在linux下编译了下,出错,哪位达人帮忙看了解决下
例子:
#include "ace/Malloc.h"
#include "ace/OS.h"

typedef char MEMORY_BLOCK;
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*;
}

void allocate_msg(const char *msg)
{
    mesg_array_=allocator_.malloc(ACE_OS::strlen(msg)+1);
    ACE_OS::strcpy(mesg_array_,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));
    exit(1);
}
int n_blocks=ACE_OS::atoi(argv);

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
页: [1]
查看完整版本: ACE内存管理