Iterator string to char *

D

david

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);
}
....
}
 
P

Paul

david said:
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
 
J

Jorge Rivera

This may not be your ptoblem, but you should be using.

(*tag).c_str(), not tag->c_str();

JLR
 
P

Pete Becker

Jorge said:
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.
 
D

david

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

}
 
J

John Harrison

david said:
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
 
D

David Harmon

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?
 
P

Paul

david said:
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
 
D

david

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,888
Members
47,420
Latest member
ZitaVos505

Latest Threads

Top