std::string displaying produces errors [message #1857638] |
Fri, 17 February 2023 10:29  |
Eclipse User |
|
|
|
My code depends on the following function for reading from a socket:
int readSocket( std::string &sockResponseString) {
auto can_read = [](int s) -> bool {
fd_set read_set;
FD_ZERO(&read_set);
FD_SET(s, &read_set);
struct timeval timeout {};
int rc = select(s + 1, &read_set, NULL, NULL, &timeout);
return (rc == 1) && FD_ISSET(s, &read_set);
};
auto do_read = [&sockResponseString](int s) -> bool {
char buf[BUFSIZ];
int nbytes = recv(s, buf, sizeof(buf), 0);
if (nbytes <= 0)
return false;
sockResponseString += std::string(buf, static_cast<size_t>(nbytes));
return true;
};
sockResponseString.clear();
bool done{};
while (!done) {
if (can_read(Master_sfd) && do_read(Master_sfd)) {
while (!done) {
if (!can_read(Master_sfd))
done = true;
do_read(Master_sfd);
}
}
}
return static_cast<int>(sockResponseString.size());
};
This function does exactly what is needed and compiles without errors.
It is strange however that I am not able to inspect the sockResponseString in the debugger.
After right-clicking this variable in the debugger and selecting 'display as array', these message appear in the lower part of the debug view:
Multiple errors reported.
1) Failed to execute MI command:
-var-create - * (*((sockResponseString)+0)@1)
Error message from debugger back end:
No symbol "operator+" in current context.
2) Unable to create variable object
3) Failed to execute MI command:
-data-evaluate-expression (*((sockResponseString)+0)@1)
Error message from debugger back end:
No symbol "operator+" in current context.
4) Failed to execute MI command:
-var-create - * (*((sockResponseString)+0)@1)
Error message from debugger back end:
No symbol "operator+" in current context.
The C++ - community could not tell me what this means.
Do I have to change my code?
Ben
|
|
|
Re: std::string displaying produces errors [message #1857648 is a reply to message #1857638] |
Sat, 18 February 2023 09:52  |
Eclipse User |
|
|
|
I don't believe that std::string can be reliably accessed as an array. A string gives you access to an arbitrary sequence of bytes but you need to use the accessor methods on the class to get at the data. It incorporates techniques such as small string optimization to can impact how to get to the actual data. In order for the debugger to be able to present something as an array, it needs a pointer to a memory location of the actual data to display. Here, sockResponseString is a reference to a std::string. It isn't a pointer so the debugger isn't able to present anything as an array.
You likely need to add a watch expression using the various string accessor methods such as the [] operator or front()
|
|
|
Powered by
FUDForum. Page generated in 0.03787 seconds