473,472 Members | 2,168 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Change default allocation of std::string

Dear NG,

I would like to change the allocator of e.g. all std::strings, without
changing my code. Is there a portable solution to achieve this?

The only nice solution I can think of, would be a namespace and another
typedef to basic_string:

namespace my_string
{
typedef std::basic_str<...> string;
....
}

but in this case I'd have to replace all std::string's with
my_string::string what is not possible right now.

Any ideas?

Thanks, regards,
Patrick
Jul 23 '05 #1
8 9155
> I would like to change the allocator of e.g. all std::strings, without
changing my code. Is there a portable solution to achieve this?
Depends on how you wrote your program. If you used fully qualified
names (std::string) everywhere, no, except by changing the header
files, which is illegal, non portable and bad for your health.
The only nice solution I can think of, would be a namespace and another
typedef to basic_string:

namespace my_string
{
typedef std::basic_str<...> string;
....

}

but in this case I'd have to replace all std::string's with
my_string::string what is not possible right now.


I'm afraid you're caught here. If your code is in a namespace and you
dumped the namespace std's content:

# include <string>

using namespace std;

namespace N
{
string s;
}

you may do

# include <string>

using namespace std;

namespace N
{
typedef std::basic_string<char, my_allocator> string;
string s;
}

which will solve your problem. Apart from this and a global
find/replace, I cannot find any more solutions.
Jonathan

Jul 23 '05 #2
Hi Jonathan,
I'm afraid you're caught here. If your code is in a namespace and you
dumped the namespace std's content:
I thought so :(.
# include <string>

using namespace std;

namespace N
{
typedef std::basic_string<char, my_allocator> string;
string s;
}

which will solve your problem. Apart from this and a global
find/replace, I cannot find any more solutions.


Same for me. But this leads me to the following conclusion:

Using the abbreviations "string" or "wstring" for basic_string<...> is
somewhat "evil" (and then not good for my health, too), because it restricts
the functionality and features of basic_string to a smaller subset. Same for
all other default-templated fixed abbreviations inside the std namespace :(.

Using own typedef's (e.g. in another namespace) guard this flexibility,
whilst I am allowed to change the typedef's. But this is only valid for my
source and I have no possibilty to affect 3rd-party-code behaviour with
simple change (like overwriting new/delete).

This is somewhat unflexible, isn't it? How do you solve this?

Regards,
Patrick
Jul 23 '05 #3
>Using the abbreviations "string" or "wstring" for basic_string<...> is
somewhat "evil" (and then not good for my health, too), because it restricts
the functionality and features of basic_string to a smaller subset. Same for
all other default-templated fixed abbreviations inside the std namespace :(. Using own typedef's (e.g. in another namespace) guard this flexibility,
whilst I am allowed to change the typedef's. But this is only valid for my
source and I have no possibilty to affect 3rd-party-code behaviour with
simple change (like overwriting new/delete). This is somewhat unflexible, isn't it? How do you solve this?


Concerning your own code, you solve this by designing it first. What
may change you factor in a single place, such as a global string
typedef. Now I understand not everything may be designed beforehand,
but that's a programmer's life, unfortunately.

Concerning 3rd party libraries, well there's nothing you can do. If
they expect plain std::string's and you cannot use the standard
allocator, you're toasted. They obviously shouldn't be used on that
project.

If the project you are working on is using plain std::strings and it
was decided in the middle of production to change the strings allocator
and they are using libraries which expect plain std::strings, that
means there was a big fuckup during design time. I hope you're not in
charge of anything. If you are, I hear they are employing in your
nearest grocery store :)

Jonathan

Jul 23 '05 #4
Patrick Kowalzick wrote:
Using the abbreviations "string" or "wstring" for basic_string<...> is
somewhat "evil" (and then not good for my health, too), because it restricts
the functionality and features of basic_string to a smaller subset. Same for
all other default-templated fixed abbreviations inside the std namespace :(.

Using own typedef's (e.g. in another namespace) guard this flexibility,
whilst I am allowed to change the typedef's. But this is only valid for my
source and I have no possibilty to affect 3rd-party-code behaviour with
simple change (like overwriting new/delete).

This is somewhat unflexible, isn't it? How do you solve this?

Regards,
Patrick

Read Pablo Halpern's allocator proposal. It addresses this very issue.

http://home.earthlink.net/~phalpern/...orProposal.pdf

-shez-

Jul 23 '05 #5
Hi Jonathan
Using the abbreviations "string" or "wstring" for basic_string<...> is
somewhat "evil" (and then not good for my health, too), because it
restricts
the functionality and features of basic_string to a smaller subset. Same
for
all other default-templated fixed abbreviations inside the std namespace
:(.
Using own typedef's (e.g. in another namespace) guard this flexibility,
whilst I am allowed to change the typedef's. But this is only valid for my
source and I have no possibilty to affect 3rd-party-code behaviour with
simple change (like overwriting new/delete).

This is somewhat unflexible, isn't it? How do you solve this?


Concerning your own code, you solve this by designing it first. What
may change you factor in a single place, such as a global string
typedef. Now I understand not everything may be designed beforehand,
but that's a programmer's life, unfortunately.


I do not agree here. When std::string came up many people were complaining
you it, but IMO there is a silent agreement for quite a long time that
std::string is not perfect but the best we can get for a long time. But now
it hits me quite hard :):

-I can not directly affect the std::string allocation.
-Even worse, if I could, I would not be able to interface 3rd party libs
with std:string. This would lead me back to write interfaces with C-Arrays?
Surely not!
Concerning 3rd party libraries, well there's nothing you can do. If
they expect plain std::string's and you cannot use the standard
allocator, you're toasted. They obviously shouldn't be used on that
project.
Toasting is not good for my health :).
If the project you are working on is using plain std::strings and it
was decided in the middle of production to change the strings allocator
and they are using libraries which expect plain std::strings, that
means there was a big fuckup during design time. I hope you're not in
charge of anything. If you are, I hear they are employing in your
nearest grocery store :)


Am I allowed to overload new/delete only for std::string? AFAIK this is not
possible, but this would work around my problem..... I have to read some
chapters in the standard.

Regards,
Patrick
Jul 23 '05 #6
Hi Shezan,

thanks for the link. Very helpful to understand allocators.

Regards,
Patrick
Jul 23 '05 #7
>> Concerning your own code, you solve this by designing it first. What
may change you factor in a single place, such as a global string
typedef. Now I understand not everything may be designed beforehand,
but that's a programmer's life, unfortunately.
I do not agree here. When std::string came up many people were complaining
you it, but IMO there is a silent agreement for quite a long time that
std::string is not perfect but the best we can get for a long time. But now
That's not what I said. I personally always favor std::string over
anything else when I can. What I said is that elements that may change
during production should have been made easy to change. If it was
determined that the string's allocator may change, the code should have
used my_string everywhere instead of std::string directly. That would
have made the modification easy by changing only the typedef from

typedef std::string my_string;

to

typedef std::basic_string<char, my_allocator> my_string;

though that does not solve the problem of libraries.
it hits me quite hard :):
Sorry to hear that :)
-I can not directly affect the std::string allocation.
-Even worse, if I could, I would not be able to interface 3rd party libs
with std:string. This would lead me back to write interfaces with C-Arrays?
Surely not!
Well that would part of the problem but would create many others, such
as functions which modify their arguments. You're right, not a good
idea.
If the project you are working on is using plain std::strings and it
was decided in the middle of production to change the strings allocator
and they are using libraries which expect plain std::strings, that
means there was a big fuckup during design time. I hope you're not in
charge of anything. If you are, I hear they are employing in your
nearest grocery store :)

Am I allowed to overload new/delete only for std::string? AFAIK this is not
possible, but this would work around my problem..... I have to read some
chapters in the standard.


No you cannot. It would require to change std::basic_string's class'
definition (illegal), which would modify all types based on that
template. Remember that std::string is a typedef for std::basic_string.
What's more, you shouldn't normally use std::strings on the heap: it's
a value type with no virtual functions.

Here's what I recommend.

If you have access to the whole code, replace all std::string by
my_string, which is a typedef for whatever you want. Unfortunately, you
cannot copy a string with a different allocator, so if your libraries
are using std::string, you'll have to interface them and that means
more modifications to your code, but you may have no choice. If you
must not use the standard allocator, you must change the libraries.

If you don't have access to the whole code, forget it.

You may receive other advices on this thread, so do not despair.

Jonathan

Jul 23 '05 #8
There is a trick that may help in some (desperate) cases. Some
compilers (e.g. GCC) give you the option to force include of
header-file at the beginning of every source-file (in GCC, with the
-include [filename]).

Now, try to do this. Create a header file like this:

/* header file which will be the first included header of every source
file */
#include <string>
#include <memory>

namespace std {
typedef char_traits<char> __my_straits;

/* Replace with your allocator: */
typedef allocator<char> __my_salloc;
typedef basic_string<char, __my_straits, __my_salloc> __my_string;
}

#define string __my_string
/* end of header */
This hack can do the work for you, but you must rebuild *all* source
files in your project with this header included. However, I would not
recommend such usage except for debug mode. Note also that if you need
to link your application against external libraries which uses
std::string, this will not work and you will get linkage errors.

I hope it helps.

shablool.

Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Espen Ruud Schultz | last post by:
Lets say I have a char pointer and an std::string. Is it possible to get a pointer to the std::string's "content" so that the char pointer can point to the same text? And vice versa; can I give...
10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
11
by: Christopher Benson-Manica | last post by:
Let's say I have a std::string, and I want to replace all the ',' characters with " or ", i.e. "A,B,C" -> "A or B or C". Is the following the best way to do it? int idx; while(...
22
by: Jason Heyes | last post by:
Does this function need to call eof after the while-loop to be correct? bool read_file(std::string name, std::string &s) { std::ifstream in(name.c_str()); if (!in.is_open()) return false; ...
6
by: Nemok | last post by:
Hi, I am new to STD so I have some questions about std::string because I want use it in one of my projects instead of CString. 1. Is memory set dinamicaly (like CString), can I define for...
2
by: FBergemann | last post by:
if i compile following sample: #include <iostream> #include <string> int main(int argc, char **argv) { std::string test = "hallo9811111z"; std::string::size_type ret;
84
by: Peter Olcott | last post by:
Is there anyway of doing this besides making my own string from scratch? union AnyType { std::string String; double Number; };
11
by: Jacek Dziedzic | last post by:
Hi! I need a routine like: std::string nth_word(const std::string &s, unsigned int n) { // return n-th word from the string, n is 0-based // if 's' contains too few words, return "" //...
4
by: Ron Garret | last post by:
Is there a way to change the default string encoding used by the string.encode() method? My default environment is utf-8 but I need it to be latin-1 to avoid errors like this: Traceback (most...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.