By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,538 Members | 909 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,538 IT Pros & Developers. It's quick & easy.

Iterator string to char *

P: n/a
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);
}
....
}

Jul 22 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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

Jul 22 '05 #2

P: n/a
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
Jul 22 '05 #3

P: n/a
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)
Jul 22 '05 #4

P: n/a
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.


Jul 22 '05 #5

P: n/a

"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
Jul 22 '05 #6

P: n/a
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?

Jul 22 '05 #7

P: n/a
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
Jul 22 '05 #8

P: n/a
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


Jul 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.