seg fault when printing string

This is a discussion on seg fault when printing string within the C Programming forums, part of the General Programming Boards category; hi, i get a seg fault when i try to print the currentSearch ...and suggestions? thanks Code: char *currentSearch = ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    29

    seg fault when printing string

    hi,

    i get a seg fault when i try to print the currentSearch ...and suggestions?

    thanks

    Code:
    	char *currentSearch = NULL;
    
    	setSearchTag("title", currentSearch);
    	printf("Currently looking for:%s", currentSearch);
    	
    	
    	
    
    void setSearchTag(char *search, char *currentSearch) {
    
    	char *startTag = "<";
    	char *endTag = ">";
    	
    	if(currentSearch == NULL || (strlen(search) + strlen(startTag) + strlen(endTag)) > strlen(currentSearch)) {
    		
    		currentSearch = malloc(sizeof(char) * (strlen(search) + strlen(startTag) + strlen(endTag)));
    		
    		if (!currentSearch){
    			fprintf(stderr, "Unable to reallocate %d bytes of memory for current search\n", strlen(search) + strlen(startTag) + strlen(endTag));
    			exit(1);
    		}
    	}
    
    	strcpy(currentSearch, startTag);
    	strcat(currentSearch,search);
    	strcat(currentSearch,endTag);
    				
    }

  2. #2
    SleepWalker tjohnsson's Avatar
    Join Date
    Apr 2004
    Posts
    70
    currentSearch memory needs to be allocated before you leave it to setSearchTag function.
    other way to fix problem is return currentSearch pointer from setSearchTag function
    like:
    char *currentSearch;
    currentSearch = setSearchTa("title");
    and print it out....
    Last edited by tjohnsson; 04-23-2004 at 03:27 AM.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    29
    cool ..thanks

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    2
    the function 'setSearchTag' doesnt update currentSearch.
    change the fn return type to (char *) & return the updated currentSearch pointer address.
    & the memory allocation also needs change :
    currentSearch = (char *)malloc(sizeof(char) * (strlen(search) + strlen(startTag) + strlen(endTag)));
    hope it 'll do.

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    29
    also 1 more thing....why is it now...when i call setSeachTag() again....it gives me a bus error
    on the malloc line?
    Code:
    	currentSearch = setSearchTag("title", currentSearch);
    	currentSearch = setSearchTag("/title", currentSearch);	
    
    char* setSearchTag(char *search, char *currentSearch) {
    
    	char *startTag = "<";
    	char *endTag = ">";
    	
    	if(currentSearch == NULL || (strlen(search) + strlen(startTag) + strlen(endTag)) > strlen(currentSearch)) {
    		
    		currentSearch = malloc(sizeof(char) * (strlen(search) + strlen(startTag) + strlen(endTag)));
    		
    		if (!currentSearch){
    			fprintf(stderr, "Unable to reallocate %d bytes of memory for current search\n", strlen(search) + strlen(startTag) + strlen(endTag));
    			exit(1);
    		}
    	}
    
    	strcpy(currentSearch, startTag);
    	strcat(currentSearch,search);
    	strcat(currentSearch,endTag);
    
    	return currentSearch;
    				
    }

  6. #6
    SleepWalker tjohnsson's Avatar
    Join Date
    Apr 2004
    Posts
    70
    Code:
    char *
    set_stag ( char *s )
    {
        char *b;
    
        if ( !s || !( b = ( char * ) malloc ( sizeof ( char ) * ( strlen ( s ) + 3 ) ) ) ){
             return null;
        }
        sprintf ( b, "<%s>", s );
    
        return  b;
    }
    if you haven't already figure out what was problem....

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    >> & the memory allocation also needs change :
    Read this: Casting malloc().
    It does need to change however, you're not adding space for a NULL terminator.

    The bus error on the malloc() line means you most likely have some bad code elsewhere (or maybe from a previous call to that function).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Can someone help with my seg fault?
    By John_L in forum C++ Programming
    Replies: 23
    Last Post: 03-01-2008, 03:04 PM
  3. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  5. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM

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