Consider this piece of code:
std::string* symbol = (std::string*) to_push->value;
printf("Got symbol\t%s\n", symbol->c_str());
switch ((symbol->operator[](0)) {
Here, to_push is a pointer to an object which has an attribute value of type void*. So i convert that value to std::string* because I need to know what is its content.
On the second line, added to get an idea of where the segfault happens, I also try printing the value of that string, which always works.
On the last line though, when I finally try comparing that string's first character over a list of possibilities, I get a segmentation fault error. You see here I'm trying to access the string through the operator [0] whitout deferencing it. I've also tried many other combinations, like *(symbol->c_str()) or (*symbol)[0], but I always get that segmentation fault error.
Why is that? What am I doing wrong?
If it can be helpful, both the objects pointed to by to_push and to_push->value are dinamycally allocated.
Aucun commentaire:
Enregistrer un commentaire