I am not sure if the title of my thread indicates where the problem is, but that is what I think it is.

void convertFormat(vector<string>& files, map<string,int>& tickerMap, vector<int>& numRecords )
{
    string txt = ".txt";
    string filePathNameString;
    for ( int k =0 ; k < (int)files.size(); k++)
    {
        filePathNameString = files[k];

        // extract ticker from filename string
        size_t start = filePathNameString.find_last_of("/");
        size_t end = filePathNameString.find_last_of("_");
        string ticker = filePathNameString.substr(start+1,end-start-1);

        char * filePathName = (char *)(filePathNameString.c_str());
        TAQTradesReader taq( filePathName );
        }
}

where the constructor is of form

TAQTradesReader::TAQTradesReader( char * filePathName )

The problem is that this function compiles and runs properly in VS, but when I run on my Cygwin, I get a "segmentation fault(core dumped)". Can someone please help me.
The thing is if I change line 15 to this then I dont get the segmentation fault

TAQTradesReader taq( "C:/Users/abhishek/Desktop/Projects/TestTAQ/TAQ/20070620/MSFT_trades.binRT" );

Recommended Answers

All 10 Replies

If it helps, memory is allocated inside the constructor of TAQTradesReader using "new" keyword and is returned in its destructor using "delete" keyword.

Why do you cast away the constness?

char * filePathName = (char *)(filePathNameString.c_str());
TAQTradesReader taq( filePathName );

Surely you are making no attempt to modify the text that you are passing?

Instead of answering your question(which is not possible with the code you provided) let's take back a step and ask yourself why do you hand over a char*? If, for some obscure reason, it is needed then Dave Sinkula's question should be answered. What .c_str() does, is 'convert' the string to a 'const char*' where 'const' is the key, meaning that you can not edit it.

Since the constructor of TAQTradesReader accepts char*, I had to convert it to that form. Otherwise I get compile time errors.

Ok I'm sorry I didn't realize you had no control over it.
But still, it probably is a problem them of constness. You are 'forcing' to convert a const char* to a char* which is never a good idea.

So try to think of a way to get a 'real' char*

Oh, Thanks a lot. I will try to think of a way to do that. Do you have any idea about that.

Yes I do, but you will learn more from it by trying yourself.

Just remember that you cant change anything to the string.

Haha, I guess you are right. I will play around with this thing. I really appreciate the help. I worked so hard on this assignment, and when I finally run on unix, it was disheartening to see it fail. I am glad now I know where the problem is, thanks to you.

std::string Name="Test";
	int NLen=Name.length();
	char* Result = new char[NLen+1];
	strcpy_s(Result, NLen,Name.c_str());

        delete Result;

I hope this is the right approach. Unless there is a simpler way out there.

I would say, give it a go in your 'real' application.
There is however one wrong thing though...

You allocate a new dynamic array of chars, but when you do the cleanup(delete) you are not telling the compiler that Result is in fact an array. The correct way to clean up is: delete[] Result

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.