Thread: Problem related to string class & lpthread on HP-UX 11iV2

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    6

    Problem related to string class & lpthread on HP-UX 11iV2

    Hi,

    I am facing this weird problem. When the sample pgm given below is compiled with -lpthread

    option, the pgm gives seg fault on HP-UX 11iv2

    The compiler used is "HP aC++/ANSI C B3910B A.05.50"
    The pgm was compiled using - "aCC -lpthread samp.cpp"

    Code:
    #include <iostream.h>
    #include <string>
    
    
    #define CONSTLM "Error occurred while accessing log directory %s. 1Reason Code = %d. Assuming 
    
    default log directory = /opt/newdirecty/log"
    
    class Msg : public exception
    {
    	string m_strMsg;	
    public:
    Msg()
    {
    }
    
    Msg(const char*p_strMsg, const char *path, int no)
    {
    	char l_str[1024];
    	memset(l_str,0,1024);
    	sprintf(l_str,p_strMsg,path,no);
    	m_strMsg = l_str;
    }
    
    void display()
    {
    	string l_str;
    	l_str ="_";
    	l_str +=m_strMsg;
    	l_str +="\n";
    	printf("Msg2 - %s\n",l_str.c_str());
    }
    };
    
    class A
    {
    	public:
    	void ab()
    	{
    		try
    		{
    			throw Msg(CONSTLM,"/opt/newdirecty/log",2);
    		
    		}
    		catch(Msg &m)
    		{
    			m.display();
    		}
    		
    	}
    
    };
    int main()
    {
    	A obj;
    	obj.ab();
    
    }
    When same pgm is compiled without using "-lpthread", it works fine.

    The problem seems to occur at display function at Line "l_str +="\n";"
    Also, instead of throw Msg obj, if a display func is directly called, it works fine.

    It seems to be a problem related to combination of string, throw & lpthread

    Has anyone faced such problem before?

    Please Help

  2. #2
    Registered User
    Join Date
    May 2006
    Location
    Berkshire, UK
    Posts
    29
    Have you tried it wouout the l_str +="\n"; line?
    Irrespective of that, it works fine for me in FC4 64. There must be something odd going on in your system.
    Out of interest, why are you using printf when you have std::cout << ........... << std::endl; available? That would be a bit more C++. Same for the sprintf too of course.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    6
    Solution Found.

    We need to include "-mt" flag too. It internally includes -lpthread

    When "aCC -mt Sample.cpp" is executed, it works properly!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM
  5. Replies: 3
    Last Post: 12-03-2001, 01:45 PM