seg fault with c string conversion

This is a discussion on seg fault with c string conversion within the C++ Programming forums, part of the General Programming Boards category; I'm getting a segmentation fault when running this code. The time displays fine so I have no idea were the ...

  1. #1
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572

    seg fault with c string conversion

    I'm getting a segmentation fault when running this code. The time displays fine so I have no idea were the seg fault is coming from. If I don't assign 's' to temp2, and just print s, it works fine - but I need a string! LOL. any thoughts?

    Code:
    time_t current_time;
    char *s;
    struct tm *loc;
    current_time = time(0);
    loc = localtime(&current_time);
    
    
    if( temp == "%t" )
    		{
    			strftime( s, 100, "%I:%M %p", loc );
    			temp2 = string(s);
    			cout << endl << temp2 << endl; //this runs fine
    		}

    and here is the whole function...still dirty though
    Code:
    void setPrompt( string newP )
    {
    	int modLoc = newP.find('%', 0); //location of %
    	string temp, temp2;
    	string txt;
    
    	time_t current_time;
    	char *s;
    	struct tm *loc;
    	current_time = time(0);
    	loc = localtime(&current_time);
    
    	if(modLoc == string::npos)
    		prompt = newP;
    	while(modLoc != string::npos)
    	{
    		if( modLoc>0 )
    		{
    			txt = newP.substr(0,modLoc-1);
    			prompt.append(txt);
    		}
    		temp = newP.substr(modLoc, 2);
    		//cout << endl << "--txt: " << txt <<"\n--temp: " << temp <<endl << "--newp: " << newP <<endl;
    		if( temp == "%/" )
    			prompt.append(PWD);
    		if( temp == "%t" )
    		{
    			//temp2 = string(getenv("time"));
    			//prompt.append(temp2);
    			strftime( s, 100, "%I:%M %p", loc );
    			temp2 = string(s);
    			cout << endl << temp2 << endl;
    		}
    		if( newP.size() > (modLoc+2) )
    		{
    			newP = newP.substr(modLoc+2, newP.length());
    			//cout << endl << "txt: " << txt <<"\ntemp: " << temp <<endl << "newp: " << newP <<endl;
    		}
    		else
    			break;
    		
    		modLoc = newP.find('%', 0);
    	}
    	
    		
    }

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    's' doesn't point to valid memory.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    ok, then why does it let me do

    temp2 = string(s)

    ? why doesn't it crash here?

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  4. #4
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    nm...figured it out. Thanks sebastiani.

    edit:: for completeness:

    I declared s like:

    char s[100];

    and it works fine, for obvious reasons - obvious now!! lol
    Last edited by axon; 12-02-2004 at 10:07 PM.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  2. Found a seg fault, but can't explain why i get it
    By misplaced in forum C++ Programming
    Replies: 11
    Last Post: 08-29-2005, 03:58 AM
  3. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  4. seg fault when printing string
    By ccoder01 in forum C Programming
    Replies: 6
    Last Post: 04-23-2004, 06:30 AM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21