Hello,
Is the code below correct ? I always get an seg fault on execution and
the debugger points to c_str() ?
Basically i want to convert the value std::string to char * within the
iterator loop.
.....
{
.....
for(std::vector<std::string>::iterator tag = tags.begin(); tag
!=tags.end(); tag++)
{
char *value = strdup(tag->c_str());
d_something..
free(value);
}
....
} 8 2696
david wrote: Hello, Is the code below correct ? I always get an seg fault on execution and the debugger points to c_str() ? Basically i want to convert the value std::string to char * within the iterator loop.
....
{ ....
for(std::vector<std::string>::iterator tag = tags.begin(); tag !=tags.end(); tag++) { char *value = strdup(tag->c_str());
d_something..
free(value); } ... }
Post a complete, small, and compilable example that demonstrates the
problem. We don't know what "d_something" is. Maybe it's the code that
you're leaving out (the d_something) is causing the problem.
The most likely reason why your debugger points to c_str() is merely a
symptom of some other problem in your code, and is not the cause of the
problem.
Paul
Paul wrote: david wrote:
Hello, Is the code below correct ? I always get an seg fault on execution and the debugger points to c_str() ? Basically i want to convert the value std::string to char * within the iterator loop.
....
{ ....
for(std::vector<std::string>::iterator tag = tags.begin(); tag !=tags.end(); tag++) { char *value = strdup(tag->c_str());
This may not be your ptoblem, but you should be using.
(*tag).c_str(), not tag->c_str();
JLR
Jorge Rivera wrote: for(std::vector<std::string>::iterator tag = tags.begin(); tag !=tags.end(); tag++) { char *value = strdup(tag->c_str());
This may not be your ptoblem, but you should be using.
(*tag).c_str(), not tag->c_str();
They do the same thing. Libraries for older compilers sometimes leave
out the operator-> because it used to give some compilers indigestion.
--
Pete Becker
Dinkumware, Ltd. ( http://www.dinkumware.com)
Thanks, I have corrected as suggested however i still get a segfault.
The class is handler class for an xml domparser using xerces.
any idea?
void DomParser::GetTags(std::vector<std::string> tags)
{
_getDocument();
_getDocumentElement();
std::cout << "Structure of the file available as a document in
memory" << std::endl;
std::cout << "Found " << tags.size() << " tags" << std::endl;
//XMLCh* tmpstr = NULL;
for(std::vector<std::string>::iterator tag = tags.begin(); tag
!=tags.end(); tag++)
{
char *query = strdup((*tag).c_str());
XMLCh* tmpstr;
XMLString::transcode(query, tmpstr ,50);
int len;
DOMNodeList *list = c_doc->getElementsByTagName(tmpstr);
DOMNode *node;
len = list->getLength();
int i;
char *name = NULL;
char *value = NULL;
// //No result found
if (len == 0) {
std::cout <<"###This entry does not contain any " << "'" << *tag <<
"'" << " element" << std::endl;
}
//Found len results
for (i=0; i< len ;i++)
{
//Returns DOMnode object
node = list->item(i);
value = XMLString::transcode(node->getTextContent());
name = XMLString::transcode(node->getNodeName());
std::cout << "Element: " << name << std::endl;
std::cout << "Value : " << value << std::endl;
}
free(query);
}//vector
}
Pete Becker wrote: Jorge Rivera wrote:
for(std::vector<std::string>::iterator tag = tags.begin(); tag !=tags.end(); tag++) { char *value = strdup(tag->c_str());
This may not be your ptoblem, but you should be using.
(*tag).c_str(), not tag->c_str();
They do the same thing. Libraries for older compilers sometimes leave out the operator-> because it used to give some compilers indigestion.
"david" <no****@home.com> wrote in message news:40**************@home.com... Thanks, I have corrected as suggested however i still get a segfault. The class is handler class for an xml domparser using xerces. any idea?
Just because the program crashes on c_str does not mean that c_str is the
problem. There is nothing wrong with the loop you posted earlier, most
likely the bug is in the mass of other code posted. void DomParser::GetTags(std::vector<std::string> tags) {
Any good reason for copying an entire vector like that? Very inefficient,
try this instead
void DomParser::GetTags(const std::vector<std::string>& tags)
{
john
On Sun, 01 Feb 2004 20:24:48 +0100 in comp.lang.c++, david
<no****@home.com> was alleged to have written: char *query = strdup((*tag).c_str()); XMLCh* tmpstr; XMLString::transcode(query, tmpstr ,50);
Who is supposed to be allocating memory for tmpstr to point at?
david wrote: Thanks, I have corrected as suggested however i still get a segfault. The class is handler class for an xml domparser using xerces. any idea?
Sorry, I don't have "xml domparser". Your problem is elsewhere, not
c_str. For example, this code should work correctly.
#include <vector>
#include <string>
#include <iostream>
#include <cstring>
class DomParser
{
public:
void GetTags(std::vector<std::string> &tags);
};
void DomParser::GetTags(std::vector<std::string>& tags)
{
std::cout << "Structure of the file available as a document in
memory" << std::endl;
std::cout << "Found " << tags.size() << " tags" << std::endl;
for(std::vector<std::string>::iterator tag = tags.begin(); tag
!=tags.end(); tag++)
{
char *query = strdup((*tag).c_str());
free(query);
}
}
int main()
{
DomParser D;
std::vector<std::string> Tags;
// Test with 1,000 tags
Tags.resize( 1000, "Tags" );
D.GetTags( Tags );
}
If this code works, then the problem are all those calls to "xml
domparser", and the way you are handling things, not c_str(). The code
I wrote mimics what you wrote without all the xml crud, and should
compile and run cleanly (strdup seems to not be prototyped in some
compiler's version of cstring or string.h, but that is secondary at this
moment).
Paul
Thanks to all for the comments.
Indeed the problem was coming from another class called Xml. Domparser
does actually overide Xml members.
I have added the destrucor in my main and it's working fine.
thanks to all.
david
Paul wrote: david wrote:
Thanks, I have corrected as suggested however i still get a segfault. The class is handler class for an xml domparser using xerces. any idea?
Sorry, I don't have "xml domparser". Your problem is elsewhere, not c_str. For example, this code should work correctly.
#include <vector> #include <string> #include <iostream> #include <cstring>
class DomParser { public: void GetTags(std::vector<std::string> &tags); };
void DomParser::GetTags(std::vector<std::string>& tags) { std::cout << "Structure of the file available as a document in memory" << std::endl; std::cout << "Found " << tags.size() << " tags" << std::endl;
for(std::vector<std::string>::iterator tag = tags.begin(); tag !=tags.end(); tag++) { char *query = strdup((*tag).c_str()); free(query); } }
int main() { DomParser D; std::vector<std::string> Tags;
// Test with 1,000 tags Tags.resize( 1000, "Tags" ); D.GetTags( Tags ); }
If this code works, then the problem are all those calls to "xml domparser", and the way you are handling things, not c_str(). The code I wrote mimics what you wrote without all the xml crud, and should compile and run cleanly (strdup seems to not be prototyped in some compiler's version of cstring or string.h, but that is secondary at this moment).
Paul This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Charles Herman |
last post by:
I have the folowing program:
#include <iostream>
#include <ext/hash_map>
using namespace std;
using namespace __gnu_cxx;
typedef hash_map<char*, long, hash<char*> > char_hash_t;
|
by: Brett Robichaud |
last post by:
What is the correct way to allocate a string:iterator using a char*
buffer? In Visual C++ 7.1 the following works fine (where pBuf is a
char*):
std::string::iterator start = pBuf;
But under...
|
by: eric |
last post by:
Hi,
I would like to build the inParams SQLJ sample procedure from the
Development Center.
if I declare a #sql public iterator SpIterat, the Development Center
complains this should be in a...
|
by: KL |
last post by:
Well, I got through those other hurdles, but now I am stuck with trying
to get an iterator to work.
I am having a problem with trying to iterate through the STL list
container that I set up. I...
|
by: KL |
last post by:
I have a problem trying to write to a binary file that I want to name
after a particular name from a set. My code for the function follows,
please excuse the horrible mistakes you may see, I am a...
|
by: Gary Wessle |
last post by:
Hi
I am trying to pass a iterator to a function, this is the iterator and
what it does
typedef vector<string>::const_iterator vs_itr;
for(vs_itr i=vect.begin(); i!=vect.end(); ++i){
x =...
|
by: Rares Vernica |
last post by:
Hi,
I am using tr1/unordered_map in my code.
My code compiled ok on g++ (GCC) 4.1.2 (Ubuntu 4.1.2-0ubuntu1). Now I
need to compile my code on g++ (GCC) 4.0.3 (Ubuntu 4.0.3-1ubuntu5).
I get...
|
by: vasili |
last post by:
hello All,
I have a simple issue.
I defined a custom container, that encloses a std::list, which in turn
holds objects that are a simple abstraction of a six position array.
Now, i would like...
|
by: vivekian |
last post by:
Hi,
I have this following class
class nodeInfo ;
class childInfo
{
public:
int relativeMeshId ;
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |