Archived

This topic is now archived and is closed to further replies.

stodge

Segmentation when inserting into STL map in Linux

Recommended Posts

I'm having weird problems inserting values into an STL map. My code is (roughly):
    
typedef std::map<HBstring, HBstring>HBparsedDataGroup; 
HBparsedDataGroup   mParsedData; 

HBstring token;
HBstring value;

token = el->GetToken(); // Don't worry where the values come from,

value = el->GetValue(); // as they're both valid, I checked.


mParsedData.insert(make_pair(token, value)); 
    
The two strings definitely have valid values, as I've checked in the debugger and using std::cout statements. I'm really stumped with this; everything seems valid, so why the problem? I'm not using STLPort as it won't compile with g++ 3.2.2, so is the STL library that comes with g++ flaky? Thanks [edited by - stodge on May 7, 2003 11:24:21 PM] [edited by - stodge on May 7, 2003 11:25:02 PM] [edited by - stodge on May 7, 2003 11:25:53 PM]

Share this post


Link to post
Share on other sites
What is HBstring? Can an HBstring be properly copied? What is make_pair? What is token? What is value?

I''ve never had a problem with GCC''s STL implementation that I can remember (and I use it pretty heavily), if that''s any assurance.

Share this post


Link to post
Share on other sites
Sorry, HBstring is just:

typedef std::string HBstring;

token and value are both HBstrings.

I just noticed that the forum code screwed up my post. I''ll see if I can fix it.

I''m wondering if I haven''t linked in the appropriate STL library correctly. I''ve never had such a problem using STL on Windows, but this is my first foray into using it on Linux.

Thanks

Share this post


Link to post
Share on other sites
I see that make_pair is a standard thing, that I didn't happen to know about... Sorry about that question.

Here's an minimal recreation of your situation that I've confirmed to work:

    
#include <iostream>

#include <map>

#include <string>

#include <utility>


typedef std::string HBstring;
typedef std::map <HBstring, HBstring> HBparsedDataGroup;

int main(void) {
HBparsedDataGroup mParsedData;
const HBstring token = "sample_token";
const HBstring value = "sample_value";

mParsedData.insert(std::make_pair(token,value));

std::cout << mParsedData["sample_token"] << std::endl;

return 0;
}


I compiled it with:

$ g++ -o mini mini.cc

Here's my GCC information, by the way:

$ g++ -v
Reading specs from /usr/lib/gcc-lib/i386-linux/3.2.3/specs
Configured with: ../src/configure -v --enable-languages=c,c++,java,f77,objc,ada
--prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info
--with-gxx-include-dir=/usr/include/c++/3.2 --enable-shared
--with-system-zlib --enable-nls --without-included-gettext
--enable-__cxa_atexit --enable-clocale=gnu
--enable-java-gc=boehm --enable-objc-gc i386-linux
Thread model: posix
gcc version 3.2.3




[edited by - Null and Void on May 7, 2003 11:39:45 PM]

Share this post


Link to post
Share on other sites
My code is in a static library which is linked with the executable. I''m wondering if this is causing the problem somehow.

Thanks for confirming the code works.

Share this post


Link to post
Share on other sites
Btw, STLPort compiles fine with 3.2.2 you just need to change some compiler path #defines in one of the configuration header files.(cant remember which but there is a post about it on the STLPort forum).

Share this post


Link to post
Share on other sites
quote:
Original post by Ziphnor
Btw, STLPort compiles fine with 3.2.2 you just need to change some compiler path #defines in one of the configuration header files.(cant remember which but there is a post about it on the STLPort forum).


I found that post and tried it out, but it didn't work for me. I'll try it again. I think the file was stl_gcc.h for the record.

It definitely does NOT work for me. I changed stl_gcc.h to replace the existing code with this:


  # endif

# if (__GNUC__ >= 3)
# if (__GNUC_MINOR__ >= 2)
# define _STLP_NATIVE_INCLUDE_PATH /usr/include/c++/3.2
# define _STLP_NATIVE_OLD_STREAMS_INCLUDE_PATH ../c++/3.2/backward
# else

# define _STLP_NATIVE_INCLUDE_PATH ../g++-v3
# define _STLP_NATIVE_OLD_STREAMS_INCLUDE_PATH ../g++-v3/backward

# endif

# elif (__GNUC_MINOR__ < 8)


Is this what you did?

[edited by - stodge on May 8, 2003 12:02:08 PM]

Share this post


Link to post
Share on other sites
Null and Void:

I tried your mini sample and it ran just fine. Weird. It must be to do with having my code in a static library, or maybe there''s something wrong with the makefile for the library.

Share this post


Link to post
Share on other sites
I was writing a reply to this earlier when my power went out (one of our walls is a little rotted and we have yet to have it repaired, and it is raining here today). Now that I remember ...

Static libraries are just packages of object files (literally, us "ar t libexample.a" and you''ll get a list of the object files within). There''s nothing different between linking a static library and linking an object file. That is highly unlikely to be causing your problems.

Much more likely, however, is that the heap (or possibly something related to the this pointer) is being corrupted before that code is ran. Unless you can recreate the problem in a small example, I''d assume that is the problem.

Share this post


Link to post
Share on other sites