February 8th, 2011

Go Ask printf() To Behave Predictably

This is one of those problems whose solution is very evident… or bitterly hard to guess depending on how much you were influenced by what you were told about it.

I’ll paste here the case

I must be getting blind, as I can’t see what’s wrong. This code works on amd64, but crashes on x86.

  1. #include <regex>
  2. using namespace std;
  3.  
  4. int main() {
  5.     char searchMe[] = “Go ask printf() to behave predictably.”;
  6.     regex rx(“^Go ask “);
  7.     cmatch found;
  8.  
  9.     if (regex_search(searchMe, found, rx))
  10.         printf(“Found it: “%s”.n”, found.str());
  11.     else
  12.         printf(“Didn’t find it.n”);
  13. }

I debugged a bit and found out that what is failing is actually the first printf, because formatting for %s is not apparently using the address of the string, but it tries to dereference its content.

Can anyone help me see what’s wrong?

Thanks

 

You will rapidly solve it… or not! It will actually depend on what it impressed you the most about the symptoms.

Category
C++

0 comments

Discussion are closed.