Hi.. I'm Carmen and I'm new on this forum...
I am dealing with a big problem... I can't figure out why it gives me these errors... I am trying to filter a text, transform the lowercase in uppercase and leave all the other characters out. Then, I am picking some random letters and replace those from the filtered text with its equivalent. After this, I am trying to manipulate the changed text in order to find the real one. But I can't get to that point cause smth messes the things out and it gives segmentation fault or it doesn't give me any errors, but it doesn't reach the end of the function.
Can you please look on my code and give me some suggestions... I feel I can't get trough and every other homework depends on this one...
Please help
[code]
In loadFile you don't need that dynamic allocation stream.open(str.c_str(), ios::in|ios::out);
You seem to misunderstand how pointers work. They store a memory address.
1 2
filtered_text = new (nothrow) char [filtered_len+1]; //you reserve memory and its address is in filtered_text
filtered_text = text.c_str(); //you change the pointer, now you lose the reference. If you want to copy the content use strcpy()
Try to avoid all the char * and use string instead. Check for memory leaks (there isn't a single delete)
Hi,
Thanks for your reply,
I tried to use delete, but I didn't know exactly where to put it cause I couldn't use it in the function(before returning the variable)... Can you tell me when and where I have to free the memory?
About the strcpy(strcnpy) method, I can;t use it because my char* is a const one.... and it gives me errors.
But thanks for telling me how c_str works. It will help me a lot in the future.
You need to delete the memory, when you stop using it and before you override the pointer.
About the strcpy(strcnpy) method, I can;t use it because my char* is a const one....
then don't make it a const one.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
constchar* filterText(fstream& stream, string str){
char* filtered_text;
filtered_text = new (nothrow) char [filtered_len+1];
//...
strcpy( filtered_text, text.c_str() ); //you want to copy the content
return filtered_text;
}
constchar* permutation(fstream& stream, string str){
constchar* filtered_text;
// filtered_text = new (nothrow) char [filtered_len+1]; //you don't need to reserve here
filtered_text = filterText(stream, str); //because you receive the address from the function
//you use filtered_text
delete [] filtered_text; //freeing the memory
}
But you could avoid all that pointer problem, if you use strings