Find the answer to your Linux question:
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: ...
  1. #1
    Just Joined!
    Join Date
    Oct 2008
    Posts
    7

    Angry 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;
    }
    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
    but I am getting the segmentation fault when printing the test failure.
    Also a warning when I compile.

    Please let me know where I am wrong...


    With Thanks,
    Veda...

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    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

  3. #3
    Linux Guru Rubberman's Avatar
    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
    Quote Originally Posted by gerard4143 View Post
    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;
    }
    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!

  4. #4
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...