Question on Memory Pooling for char*

N

nebiyou1

I am trying to implement a simple memory pooling (simple segregated
storage) for character strings (char*) of fixed size. I am running into
some issues, however.


Here is an oversimplified example:

The following allocates a space to hold chunk number of char's.
It then builds a link list and adds the chunks to the list.


std::size_t chunk = 10;
char* array = alloc_mem.allocate(chunk);
for(size_t i=0;i!=chunk;++i)
add_to_freelist(&array);
at the end of this we will have sometn like
[char0] [char1] [char2] ... [char9]

If char is replaced by T, I understand I can use this for any object
type T. (the above example can be found on 761 of C++ primer 4th
edition)

Now, I would like to do something similar for character strings instead
of characters. In other words, given the cstring size, I would like to
allocate space to hold chunk number of cstrings with the given size. If
the size is 3, for instance

[char0[3]] [char1[3]] [char2[3]]... [char9[3]]

How do I do this? Can you please explain using the example I posted
above.

Please help!

FYI: on simple segregated storage
http://www.boost.org/libs/pool/doc/concepts.html
 
N

nebiyou1

In the above, by alloc_mem is declared as std::allocator<char>
alloc_mem; which is equivalent to
char* temp = static_cast<char*> (operator new[] (chunk))
 
J

Jeffrey Baker

In the above, by alloc_mem is declared as std::allocator<char>
alloc_mem; which is equivalent to
char* temp = static_cast<char*> (operator new[] (chunk))



I am trying to implement a simple memory pooling (simple segregated
storage) for character strings (char*) of fixed size. I am running into
some issues, however.


Here is an oversimplified example:

The following allocates a space to hold chunk number of char's.
It then builds a link list and adds the chunks to the list.


std::size_t chunk = 10;
char* array = alloc_mem.allocate(chunk);
Can you explain what this is. I would think-
char *array = new alloc_mem.allocate(chunk);
Can you give more explicit code?

for(size_t i=0;i!=chunk;++i)
add_to_freelist(&array);
at the end of this we will have sometn like
[char0] [char1] [char2] ... [char9]

If char is replaced by T, I understand I can use this for any object
type T. (the above example can be found on 761 of C++ primer 4th
edition)

Now, I would like to do something similar for character strings instead
of characters. In other words, given the cstring size, I would like to
allocate space to hold chunk number of cstrings with the given size. If
the size is 3, for instance

[char0[3]] [char1[3]] [char2[3]]... [char9[3]]

How do I do this? Can you please explain using the example I posted
above.

Please help!

FYI: on simple segregated storage
http://www.boost.org/libs/pool/doc/concepts.html

Regards,
Jeff
 
N

NebiyouGirma

Jeff,
You are right. Except in case of a c string the size of the string
matters.
char* array = new alloc_mem.allocate(chunk)
will returns a space to hold chunk number of characters.

therefore,
The solution for a string would be something like this...
char* array = alloc_mem.allocate(chunk*sz);
for(size_t i=0;i!=chunk;++i)
add_to_freelist(&array[i*sz]);

where sz is the size of the string. each chunk is has a size of sz
*sizeof(char)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top