Why is my string losing its value?

For some reason, the value of my toCheck variable is erased, and I have no idea why. Any suggestions?

bool check(string toCheck){ printf("toCheck: %s\n", toCheck.c_str()); ifstream list; list.open("list.txt"); string temp; while(list){ getline(list,temp); printf("toCheck: '%s' temp: '%s'\n",toCheck.c_str(), temp.c_str()); if(temp == toCheck){ printf("Username exists\n"); return false; } } printf("returning true\n"); return true; } 

Here's what gets passed: TestTrevor

And here is the conclusion:

 toCheck: TestTrevor toCheck: '' temp: 'Trevor' toCheck: '' temp: '' Username exists 
+6
source share
1 answer

From your comments:

It is very hard to debug (which is why I use printf ) because I fork and use processes (this is the server for the VoIP project I'm working on) and gdb did not work when I tried to monitor the child process.

Emphasis is mine.

I would not be surprised if the memory dynamically allocated for toCheck never got into a forked process or was not done, but was somehow discarded / overwritten.

NEW INFO: if I comment on getLine(list, temp); then it does not erase toCheck , any thoughts?

This is the first time in your program that std::allocator is required for the actual allocation of memory.


STL has never been developed with markup, so this is quite possible than simply does not work in this utility.

You can check what happens with the debugger. See What address memory is allocated for toCheck , and what happens when memory is allocated for temp , but it is deeply submerged.

Since it seems like you have problems with gdb, you can try resetting the addresses first ( printf("%x", &toCheck[0]); ).

+4
source

Source: https://habr.com/ru/post/913993/


All Articles