473,320 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

MemoryPool Failing from Efficient C++

'm currently fighting my way through an example of Single-Threaded Variable-Size Memory Manager in a chapter of the Efficient C++ book by Dov Bulka &David Mayhew. Unfortuantely, I'm starting to get code that won't compile and I am having a hard time finding an online errata or contacting the authors.

For those familiar with the book, Chapter 6 covers rolling your own
memory manager.

My compile errors are with the lines saying
"error C2143: syntax error : missing ';' before '<'"
"error C4430: missing type specifier - int assumed. Note: C++ does not support default-int '.

I'd love to be able to figure this out, but sadly, that's why I'm
reading the book; I am not sure how this is failing.
and in the end ,I want to write a memorypool which can support Multithreaded and Variable-Size Memory Manager Any help would be appreciated.

- wzcxjiao

Expand|Select|Wrap|Line Numbers
  1. #ifndef __MEN113_H__
  2. #define __MEN113_H__
  3. class MemoryChunk {
  4. public:
  5.     MemoryChunk (MemoryChunk *nextChunk, size_t chunkSize);
  6.     ~MemoryChunk() {delete[] mem; };
  7.  
  8.     inline void *alloc (size_t size);
  9.     inline void free (void* someElement);
  10.  
  11.     // Pointer to next memory chunk on the list.
  12.     MemoryChunk *nextMemChunk()   {return next;}
  13.  
  14.     // How much space do we have left on this memory chunk?
  15.     size_t spaceAvailable()
  16.           { return chunkSize -   bytesAlreadyAllocated; }
  17.  
  18.     // this is the default size of a single memory chunk.
  19.     enum { DEFAULT_CHUNK_SIZE = 4096 };
  20. private:
  21.     MemoryChunk *next;
  22.     void        *mem;
  23.  
  24.     // The size of a single memory chunk.
  25.     size_t chunkSize;
  26.  
  27.     // This many bytes already allocated on the current memory chunk.
  28.     size_t bytesAlreadyAllocated;
  29. };
  30.  
  31. MemoryChunk::MemoryChunk(MemoryChunk *nextChunk, size_t reqSize)
  32. {
  33.     chunkSize = (reqSize > DEFAULT_CHUNK_SIZE) ?
  34.                  reqSize : DEFAULT_CHUNK_SIZE;
  35.     next = nextChunk;
  36.     bytesAlreadyAllocated = 0;
  37.     mem = new char [chunkSize];
  38. }
  39.  
  40. //MemoryChunk :: ~MemoryChunk() { delete [] mem; }// 
  41.  
  42.  
  43. void* MemoryChunk :: alloc (size_t requestSize)
  44. {
  45.     void *addr = reinterpret_cast <void*>
  46.         //(reinterpret_cast<size_t>( mem) + bytesAlreadyAllocated);
  47.        //(static_cast 
  48.        ( _msize( mem) + bytesAlreadyAllocated);      bytesAlreadyAllocated += requestSize;
  49.     return addr;
  50. }
  51.  
  52.  
  53. inline void MemoryChunk :: free (void *doomed) {}
  54.  
  55. class ByteMemoryPool {
  56. public:
  57.     ByteMemoryPool (size_t initSize =
  58.                     MemoryChunk::DEFAULT_CHUNK_SIZE);
  59.     ~ByteMemoryPool ();
  60.  
  61.     // Allocate memory from private pool.
  62.     inline void *alloc (size_t size);
  63.  
  64.     // Free memory previously allocated from the pool
  65.     inline void free (void* someElement);
  66. private:
  67.     // A list of memory chunks. This is our private storage.
  68.     MemoryChunk *listOfMemoryChunks;
  69.  
  70.     // Add one memory chunk to our private storage
  71.     void expandStorage(size_t reqSize);
  72. };
  73.  
  74. // Construct the ByteMemoryPool object. Build the private storage.
  75. ByteMemoryPool :: ByteMemoryPool (size_t initSize)
  76. {
  77.     expandStorage(initSize);
  78. }
  79.  
  80.  
  81.  
  82. ByteMemoryPool :: ~ByteMemoryPool ()
  83. {
  84.     MemoryChunk *memChunk = listOfMemoryChunks;
  85.  
  86.     while (memChunk) {
  87.             listOfMemoryChunks = memChunk->nextMemChunk();
  88.             delete memChunk;
  89.             memChunk = listOfMemoryChunks;
  90.             }
  91. }
  92.  
  93.  
  94.  
  95. void* ByteMemoryPool :: alloc (size_t requestSize)
  96. {
  97.     size_t space = listOfMemoryChunks->spaceAvailable();
  98.  
  99.     if ( space < requestSize ) {
  100.        expandStorage(requestSize);
  101.        }
  102.  
  103.     return listOfMemoryChunks->alloc(requestSize);
  104. }
  105.  
  106. inline
  107. void ByteMemoryPool :: free (void *doomed)
  108. {
  109.     listOfMemoryChunks->free(doomed);
  110. }
  111.  
  112. void ByteMemoryPool :: expandStorage(size_t reqSize)
  113. {
  114.    listOfMemoryChunks = new MemoryChunk(listOfMemoryChunks, reqSize);
  115. }
  116.  
  117. class Rational {
  118. public:
  119.     Rational (int a = 0, int b = 1 ) : n(a), d(b) {}
  120.  
  121.     void *operator new(size_t size) {return memPool->alloc(size);}
  122.     void operator delete(void *doomed,size_t size)
  123.                   {memPool->free(doomed);}
  124.  
  125.     static void newMemPool() { memPool = new ByteMemoryPool;}
  126.     static void deleteMemPool() { delete memPool; }
  127. private:
  128.     int n; // Numerator
  129.     int d; // Denominator
  130.  
  131.     static ByteMemoryPool *memPool;
  132. };
  133. #endif
  134.  
  135.  
  136. // the main file
  137. #include <windows.h>
  138. #include "iostream"
  139. #include"men113.h"
  140. //#include <vector>
  141. using namespace std;
  142.  
  143.  
  144. MemoryPool <Rational> *Rational::memPool = 0;// the failing
  145.  
  146. int main()
  147. {
  148.     //...
  149.     Rational *array[1000];
  150.  
  151.     Rational::newMemPool();
  152.  
  153.     // Start timing here
  154.  
  155.     for (int j = 0; j < 500; j++)   {
  156.          for (int i = 0; i < 1000; i++)   {
  157.               array[i] = new Rational(i);
  158.               }
  159.          for (int i = 0; i < 1000; i++)   {
  160.               delete array[i];
  161.               }
  162.          }
  163.  
  164.     // Stop timing here
  165.  
  166.     Rational::deleteMemPool();
  167.  
  168.    // ...
  169.     return 0;
  170. }
  171.  
Jan 16 '11 #1
5 2237
horace1
1,510 Expert 1GB
the class MemoryPool does not appear to be in men113.h
Jan 16 '11 #2
I think it may be ByteMemoryPool,but in the book it was MemoryPool!although there is no MemoryPool,there is something wrong esle!can you give me right one?thank you!
Jan 16 '11 #3
weaknessforcats
9,208 Expert Mod 8TB
You just need to create memPool correctly:

Expand|Select|Wrap|Line Numbers
  1. //MemoryPool <Rational> *Rational::memPool = 0;// the failing 
  2. ByteMemoryPool* Rational::memPool = 0;
Jan 16 '11 #4
I do it as you said,but when I debug,it says:

Unhandled exception at 0x00411ad9 in mem113.exe: 0xC0000005: Access violation writing location 0x00001000.
Jan 17 '11 #5
Banfa
9,065 Expert Mod 8TB
That is because when the code calls ByteMemoryPool::expandStorage on construction of the ByteMemoryPool that method assumes that ByteMemoryPool::listOfMemoryChunks contains a valid value when it creates a new MemoryChunk.

However ByteMemoryPool::listOfMemoryChunks is never initialised by any code so it presumably contains a random value which is unlikely to lead to good operation.
Jan 17 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Hanzo | last post by:
I'm currently fighting my way through an example of Single-Threaded Memory Pooling in a chapter of the Efficient C++ book by Dov Bulka & David Mayhew. Unfortuantely, I'm starting to get code that...
0
by: Sundeep Gawande | last post by:
I am facing a strange problem. I am trying to host a .NET(C#) control in a VC6 COM ocx. Now when I create the control using CreateControl(...) or CoCreateInstance(...) my call is failing with...
6
by: idesilva | last post by:
Hi, I have an application which sends/receives messages at a very high rate. This app needs to 'log' the contents of these messages. Since the msg rate is high, 'logging' each and every msg to a...
3
by: Laurie | last post by:
Hi, I need to be able to manipulate field values within a structure using FieldInfo.GetValue and FieldInfo.SetValue, in VB.Nt 2003. The GetValue is working fine and makes it really easy for me...
18
by: Scott David Daniels | last post by:
There has been a bit of discussion about a way of providing test cases in a test suite that _should_ work but don't. One of the rules has been the test suite should be runnable and silent at every...
2
by: Raoul Watson | last post by:
I have used isNull statement for as long as I have used VB.. Recently I am devugging a program and it is very clear that the "IsNull" function sometimes would return a true even when the value is...
15
by: greg.landrum | last post by:
After using numeric for almost ten years, I decided to attempt to switch a large codebase (python and C++) to using numpy. Here's are some comments about how that went. - The code to...
21
by: py_genetic | last post by:
Hello, I'm importing large text files of data using csv. I would like to add some more auto sensing abilities. I'm considing sampling the data file and doing some fuzzy logic scoring on the...
3
by: noon | last post by:
I'm runing an xmlHttpRequest to get the site's source code and then applying the regex xhr.responseText.split(/<body*>((?:.|\n)*)<\/body>/i) Works for google.com. Fails on yahoo.com and...
144
by: dominantubergeek | last post by:
Hello, I'm a highly experienced expert C programmer and I've written this code to reverse a string in place. I think you could all learn something from it! int reverse(char* reverseme){ int...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.