Hello,
I have currently a piece a code which look like this:
foo.h
-------------------
extern const std::string NotFound;
foo.cxx
-------------------
const std::string NotFound = "Not Found";
The current approach allow me to write a function that always return a
*reference* to a string when searching through a std::map<std::s tring,
std::string> even when the key is not found.
const std::string &FindInMap(std: :string const &key);
Unfortunately it creates some side effect. Namely if you compile the
library in static libA.a, then create a shared library that links to
this static library libB.so. Then when trying to dlopen/dlclose the
shared lib libB.so, a seg fault occurs when destroying those global symbols.
Any comments on a better approach/solution ?
Thanks,
Mathieu 3 2340
On Mon, 17 Oct 2005 15:00:41 GMT, Mathieu Malaterre
<mm**********@M nycap.rr.com> wrote: Any comments on a better approach/solution ?
Return an object, not a reference. For global variables, there is no
guarantee about order of initialization, and therefore order of
destruction -- as you have seen -- is also not always as expected.
--
Bob Hairgrove No**********@Ho me.com
Mathieu Malaterre wrote: Hello,
I have currently a piece a code which look like this:
foo.h ------------------- extern const std::string NotFound;
foo.cxx ------------------- const std::string NotFound = "Not Found";
The current approach allow me to write a function that always return a *reference* to a string when searching through a std::map<std::s tring, std::string> even when the key is not found.
const std::string &FindInMap(std: :string const &key);
Unfortunately it creates some side effect. Namely if you compile the library in static libA.a, then create a shared library that links to this static library libB.so. Then when trying to dlopen/dlclose the shared lib libB.so, a seg fault occurs when destroying those global symbols.
Any comments on a better approach/solution ?
Thanks, Mathieu
Well, turning your global string into some sort of singleton might help.
[But then, maybe not.]
If you write:
const std::string& NotFound() {
const static std::string not_found = "String not found!";
return not_found;
}
then you have some control over your string's creation. I'm assuming --
but in no way whatsoever know, or have reason to think I know -- that
your dynamic library is horking because of something involved in
creating your global string.
But I'll add my askance look to the other reply; the solution above is
/evil/. Do you /need/ evil, or do you /want/ evil? If your program is
not bottlenecking around string-copying, it will execute in microseconds
whether or not you copy a string here and there. And heck, what's wrong
with iterators? Use iterators, love iterators.
Mathieu Malaterre wrote: Hello,
I have currently a piece a code which look like this:
foo.h ------------------- extern const std::string NotFound;
foo.cxx ------------------- const std::string NotFound = "Not Found";
The current approach allow me to write a function that always return a *reference* to a string when searching through a std::map<std::s tring, std::string> even when the key is not found.
const std::string &FindInMap(std: :string const &key);
Unfortunately it creates some side effect. Namely if you compile the library in static libA.a, then create a shared library that links to this static library libB.so. Then when trying to dlopen/dlclose the shared lib libB.so, a seg fault occurs when destroying those global symbols.
Any comments on a better approach/solution ?
Having FindInMap return a reference to a std::string is probably not a
worthwhile optimization. For one, it requires clients to accept the
resulting string by reference to be effective. And reference variables
in a program can be awkward to deal with. Furthermore, if the FindInMap
routine can take advantage of the named value return optimization
(NVRO), then returning a std::string by value would be more efficient.
So I would recommend changing FindInMap to return a std::string by
value. The program could then construct the not found string as needed
from a "not found" literal.
Just to be safe I would measaure the difference in performance after
this change. Personally, I would have to see a measurable hit on
performance before I would want to deal with global life spans, shared
libraries, and static destructor chains.
Greg This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Mathieu Malaterre |
last post by:
Hello,
This thread follow my previous one on the gcc mailing list. Basically I
-still- have a problem in my code. I define in a header file:
static const std::string foo = "bar";
Which not only seems to be a bad practice but also creates a bug on
MacOSX panther (gcc 3.3).
|
by: Matthias |
last post by:
Now I am confused.
I am doing a "less" comparison in my program between strings, to see
which strings in some range precede others.
This is my approach:
class SortedByName
{
public:
bool operator() (const boost::filesystem::path& lhs,
const boost::filesystem::path& rhs) {
|
by: Patrick Kowalzick |
last post by:
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
{
|
by: Divick |
last post by:
Hi all,
I have a problem related to std::string class. Is it ok to
assign a global string variable to a local string object as shown
below?
I am trying to print the address of local string buffer and then I
print the address of global string buffer and come out to be same, but
I assume that as soon as func method has returned that temporaryString
string must have been deleted and thus the buffer that it might be
holding might get...
|
by: Fei Liu |
last post by:
In Accellerated C++, the author recommends that in a header file one should
not declare
using std::string, using std::vector etc instead one should directly specify
the namespace specifier in code.
for example, this is bad practice:
header.h
#include <string>
using std::string;
| |
by: daroman |
last post by:
Hi Guys,
i've problem with my small C++ programm. I've just small template
class which represetns a array, everything works fine up to
combination with std::string. I did tried it with M$ VC++ and with GCC
(Cygwin and Linux) and my problem is when i try do this
int main(int argc, char **argv) {
array<std::stringa(10);
a = "Huhuhu"; <--- with gcc i got a crash !
|
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 ""
// 'words' are any sequences of non-whitespace characters
// leading, trailing and multiple whitespace characters
// should be ignored.
|
by: Olaf |
last post by:
Hi,
I wrap a legacy C library, e.g. the signature is
void set_error_buffer(char* buf);
where the buf length should be of length of 512 (it's defined).
Now I want to wrap it with std::string. What is the prefered way?
|
by: Travis |
last post by:
Is there an easy to convert from UnicodeString to string or char *?
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
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 we have to send another system
| |