Results 1 to 4 of 4
Hi friends,
I am trying to write automated testcases for my project code coverage,
I have a macro which prints the test passed or failure status of test case.
Code:
...
- 02-25-2010 #1Just Joined!
- Join Date
- Oct 2008
- Posts
- 7
String usage : Segmentation fault
Hi friends,
I am trying to write automated testcases for my project code coverage,
I have a macro which prints the test passed or failure status of test case.
Code:#include <iostream> #include <string.h> using namespace std; #define SCTP_ASSERT(condition1,message) \ ( failIf(!(condition1), \ message ) ) void failIf(bool shouldFail,string message ) { printf("Debug 1\n"); if ( shouldFail ) { printf("Debug 2\n"); printf("\n********** Test Failed : %s *********\n", message); exit(0); } printf("Debug 3\n"); } int main () { string str1; str1 = "Equality Test"; // c-string SCTP_ASSERT((10 == 20),str1); return 0; }but I am getting the segmentation fault when printing the test failure.Code:> g++ macro.cc execution gcc-3.4.3/g++ (aricent/rncenv/linux) macro.cc: In function `void failIf(bool, std::string)': macro.cc:16: warning: cannot pass objects of non-POD type `struct std::string' through `...'; call will abort at runtime > ./a.out Debug 1 Debug 2 Segmentation fault
Also a warning when I compile.
Please let me know where I am wrong...
With Thanks,
Veda...
- 02-25-2010 #2
You should really decide, do you want to write a program in C or C++...
I think this is what you want...I think.
Code:#include <iostream> #include <string.h> using namespace std; #define SCTP_ASSERT(condition1,message) \ ( failIf(!(condition1), \ message ) ) void failIf(bool shouldFail,string message ) { cout<<"Debug 1\n"; if ( shouldFail ) { cout<<"Debug 2\n"; cout<<"\n********** Test Failed : *********\n"<<message; } cout<<"Debug 3\n"; } int main () { string str1; str1 = "Equality Test"; // c-string SCTP_ASSERT((10 == 20),str1); return 0; }Make mine Arch Linux
- 02-25-2010 #3Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
Actually, you should use endl manipulators instead of embedded new-lines in C++ output code. To wit:
Code:#include <iostream> #include <string.h> using namespace std; #define SCTP_ASSERT(condition1,message) \ ( failIf(!(condition1), \ message ) ) void failIf(bool shouldFail,string message ) { cout << "Debug 1" << endl; if ( shouldFail ) { cout << "Debug 2" << endl << endl << "********** Test Failed : *********" << endl <<message << endl; } cout << "Debug 3" << endl; } int main () { string str1 = "Equality Test"; SCTP_ASSERT((10 == 20),str1); return 0; }Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 02-25-2010 #4
Rubberman is correct. The problem is that printf expects the '%s' to be a C string (that is, an array of characters), while you were using a C++ string (which is an object). Your original code would have worked if you invoked the c_string method on the "message" variable (at least, I think that's what it's called...I haven't done C++ in a really long time).
The only other thing that strikes me is that you shouldn't include string.h. In C++, to include the C standard library, you should include "cstring". This applies to other C standard libraries too ("cstdio", "cctypes", etc.).DISTRO=Arch
Registered Linux User #388732


Reply With Quote
