help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

64-bit weirdness with STL?


From: Robert Heller
Subject: 64-bit weirdness with STL?
Date: Thu, 25 Feb 2010 16:14:24 -0600

I have some code that is crashing with bad pointer problems.  The code,
as far as I can tell, is not doing anything that is wrong.  The code
compiles and runs just fine on a 32-bit system (same compiler, same
version of libstdc++).  It compiles just fine on the 64-bit machine, but
crashes somewhat randomly with pointer problems.  The problem seems to
be specific to the basic_string template class.

I am using CentOS 5.4 on both machines:

gollum.deepsoft.com% uname -a
Linux gollum.deepsoft.com 2.6.18-164.11.1.el5 #1 SMP Wed Jan 20 07:39:04
EST 2010 i686 i686 i386 GNU/Linux
sauron.deepsoft.com% uname -a
Linux sauron.deepsoft.com 2.6.18-164.11.1.el5xen #1 SMP Wed Jan 20
08:06:04 EST 2010 x86_64 x86_64 x86_64 GNU/Linux

And the same *stock* CentOS C++ compiler and STL library:

gollum.deepsoft.com% rpm -q --qf '%{NAME}-%{VERSION}.%{RELEASE}.%{ARCH}\n' 
gcc-c++ libstdc++ libstdc++-devel
gcc-c++-4.1.2.46.el5_4.2.i386
libstdc++-4.1.2.46.el5_4.2.i386
libstdc++-devel-4.1.2.46.el5_4.2.i386
sauron.deepsoft.com% rpm -q --qf '%{NAME}-%{VERSION}.%{RELEASE}.%{ARCH}\n' 
gcc-c++ libstdc++ libstdc++-devel
gcc-c++-4.1.2.46.el5_4.2.x86_64
libstdc++-4.1.2.46.el5_4.2.x86_64
libstdc++-4.1.2.46.el5_4.2.i386
libstdc++-devel-4.1.2.46.el5_4.2.x86_64

Is there something I should *specificly* look for?

I changed 

        line += ","+trim(buffer);
to
        line = line + "," + trim(buffer);

In one place (it seemed that the += operator for string was misbehaving).
This only delayed the crash to later in the code.

line and buffer are basic_string objects.  Trim is:

/*
 * Trim excess whitespace characters from both ends of a string.
 */
             
const string System::whitespace = " \t\r\n";
 
string System::trim(string line) const {
        string::size_type pos1, pos2;
        for (pos1 = 0; pos1 < line.size() &&
                       whitespace.find(line[pos1]) != string::npos;pos1++) ;    
        for (pos2 = line.size();
             pos2 > pos1 &&
             whitespace.find(line[pos2-1]) != string::npos;pos2--) ;
        return line.substr(pos1,pos2-pos1);
}

I have these additional functions which seem to be involved somehow:

/*
 * Read the next line from a datafile, skipping any intervening comment lines.
 * Save the line of text and return true if successfull.  If EOF reached,
 * generate and error and return false.
 * 
 */

bool System::SkipCommentsGets(istream &stream,string& buffer,const char 
*message, char **outmessage) {
#ifdef DEBUG
        cerr << "*** System::SkipCommentsGets()" << endl;
#endif
        string trimString;
        while (getline(stream,buffer,'\n')) {
#ifdef DEBUG
                cerr << "*** System::SkipCommentsGets: buffer = {" << buffer << 
"}" << endl;
#endif
                trimString = trim(buffer);
#ifdef DEBUG
                cerr << "*** System::SkipCommentsGets: trimString = {" << 
trimString << "}" << endl;
#endif
                if (trimString.size() > 0 && trimString[0] != '\'') return true;
        }
        if (outmessage != NULL) {
                *outmessage = new char[strlen(message)+1];
                strcpy(*outmessage,message);
        }
        return false;
}

/*
 * Split a string on the specified delimeter and return a vector of strings.
 */

vector<string> System::split(string s,char delimiter) const
{
        vector<string> result;
        string::size_type start, end;
#ifdef DEBUG
        cerr << "*** System::split(" << s << "," << delimiter << ")" << endl;
#endif
        for (start = 0;start != string::npos;start = end+1) {
                end = s.find(delimiter,start);
#ifdef DEBUG
                cerr << "*** System::split: start = " << start << ", end = " << 
end << endl;
#endif
                if (end == string::npos) {
                        result.push_back(s.substr(start));
                        return result;
                } else {
                        result.push_back(s.substr(start,end-start));
                }
        }
        return result;
}

In all cases I am careful to use the proper types (eg
string::size_type) and whatnot as provided by the headers and I'm not
casting between pointers and ints (of any size!).

-- 
Robert Heller             -- 978-544-6933
Deepwoods Software        -- Download the Model Railroad System
http://www.deepsoft.com/  -- Binaries for Linux and MS-Windows
heller@deepsoft.com       -- http://www.deepsoft.com/ModelRailroadSystem/
                                                                                
                                   


reply via email to

[Prev in Thread] Current Thread [Next in Thread]