I have this section of code which is causing me trouble, more specifically a segmentation fault:
...
if(tokens[0]=="angle" || tokens[0]=="Angle")
{
//create a string * that will temporarily hold the angle
string * spAngle_temp = new string[tokens[1].size()];
spAngle_temp = &tokens[1];
//convert the string hold angle to an int
stringstream ss(*spAngle_temp);
int iAngle;
ss >> iAngle;
cout << "iAngle: " << iAngle << endl;
//delete string * to free up memory
delete [] spAngle_temp;
}
...
The code works fine except for the final line "delete [] spAngle_temp"
I have tried removing the "[]", but no luck, if I remove this line it works, but really I should keep it to avoid memory leaks.
Thanks
Even if you comment that line, you have a much more serious memory leak elsewhere.
string * spAngle_temp = new string[tokens[1].size()];
Here, you're creating an array of strings with a length equal to the length of another string. I can't see the point of this.
spAngle_temp = &tokens[1];
This creates tokens[1].size() memory leaks. The array you created in the line above is no longer freeable.
delete [] spAngle_temp;
spAngle_temp is, at this point, pointing to tokens[1]. Since tokens is an std::vector<std::string>, the string at that point is static, so deleting it causes a segmentation fault.
PS: I might have sounded a bit harsh. I just forgot for a moment on which forum I was posting.