Thread: Segmentation fault on strcat

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    6

    Segmentation fault on strcat

    I have been going crazy trying to get this code to work for days now.

    all i want to do is compare two strings.

    when i do a strcmp, it throws a segmentation falult

    i could really use some help with my code.

    my main function looks like this:
    Code:
    int main(int argc, char *args[]){
    	
    	//char  ch1[] = "word";
    	//char  ch2[] = "(,)";
    	//char * result;
    	//printf("here\n");
    	//strcat(ch1,ch2);
    	//printf("printf: %s\n", ch1);
    	
    	///*
    	start_database();
    	char buffer[250];
    	char *p;
    	printf("please enter in a command:");
    	while(fgets(buffer, sizeof(buffer), stdin)!=NULL){
    		printf("%s\n",run_database_command(buffer));
    		printf("\n");
    		printf("please enter in a command:");
    	}
    	//*/
    	return 0;
    }
    and my code looks like this... look for the comment where it fails:

    Code:
    char * run_database_command(char * command){
    	if(!db_on) return "Database not on";
    	
    	int num;
    	char c1[100]= malloc(strlen(command));
    	char ** tokens;
    	if(strpos(command, ",")==-1 && strpos(command, "(")==-1){
    		snprintf(c1, sizeof(c1),"%s",command);
    	}
    	else{
    		tokens = split( command, &num, ",()");
    		snprintf(c1, sizeof(c1),"%s",tokens[0]);
    	}
    	
    	printf("crazy %s\n", c1);
    	printf("crazy %s\n", strcmp(c1,"help")); // FAILS HERE!!!!!!!!!!!!!!!!!!!!!!
    	///*
    	if(! strcmp(c1, "help")){
    	return "--commands--\n help - shows help screen\n show - shows contents of database\n insert(title, description, quanty) - this commands inserts a item into the database. \n delete(id) - deletes item by id. use show or search to get id. search(title) - search for exact match title. \n update(id, feild, value) - where feild is title description or quanty. \n to exit, <clt> + c\n";
    	}
    	else if(! strcmp(c1, "show")){
    		return showdata();
    	}
    	else if(! strcmp(c1, "insert")){
    		return insert(tokens[1],tokens[2],tokens[3]);
    	}
    	else if(! strcmp(c1, "update")){
    		return update(atoi(tokens[1]),tokens[2],tokens[3]);
    	}
    	else if(! strcmp(c1, "delete")){
    		return deleted(atoi(tokens[1]));
    	}
    	else if(! strcmp(c1, "search")){
    		return search(tokens[1]);
    	}
    	for(int i=0;i<num;i++) free(tokens[i]);
    	free(tokens);
    	free(command);
    	free(num);
    	free(c1);
    
    	return "something went wrong.. type help";
    	//*/
    }

    please let me know if you need to see more code

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    There are plenty of other issues, but yes
    Code:
    printf("crazy %s\n", strcmp(c1,"help")); // FAILS HERE!!!!!!!!!!!!!!!!!!!!!!
    strcmp() returns an int but printf interprets that int as a pointer to char ( %s format ) -> boom.
    Kurt

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    40
    Note that strcmp() does not perform bounds checking, and thus risks overrunning str1 or str2. For a similar (and safer) function that includes bounds checking, see strncmp().


    #include <string.h>
    int strncmp( const char *str1, const char *str2, size_t count );

    compares at most count characters of str1 and str2
    If there are less than count characters in either string, then the comparison will stop after the first null termination is encountered

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Code:
    char c1[100]= malloc(strlen(command));
    I have a hard time believing that even compiles.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    6

    Post thank you

    thank you. this works now.

    but now im getting: aborted core dump errors.


    i am attaching my code. if someone can fix this i will be truly grateful.

    I have been banging my head against this program for so long, im sick of looking at it.

    the program goal is to read and store information from a file.. like a database.

    remove .txt from the dat file and make file. look in make file to see how it compiles.

    run data_client and try it out. type help.. or show. or insert(title of item, details of item, 2)

    my delete is wrong.. i know. if someone can make a function for me that would be great.
    update may work.
    and search throws me a error.
    please give it a run and let me know what to do.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >
    Code:
    >		for(c=i,pos=0;c<i+searchsize && i+searchsize<stringsize;c++,pos++)
    >			buffer[pos] = str[c];
    >		buffer[c] = '\0';
    I would think the last line above should be:
    Code:
    		buffer[pos] = '\0';
    Code:
    char * showdata(){
    	
    	char output[2000];
    .
    .
    	return output;
    }
    You are returning a local array (output). You can't do that. The address of this local array will cease to exist upon return from this function. This is what I noticed at a quick glance.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >
    >int strpos( char *str, char *search )
    >{
    .
    .
    >	buffer=malloc(sizeof(*search));
    This only allocates space for 1 char. This should probably be:
    Code:
    	buffer = malloc(searchsize+1);

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    6

    Unhappy

    thank you swoopy for you help.

    has anyone tryed runing the code. i could realy use a leg up on getting this thing done. if i had all the time in the world i would sit down with a programming in C book.. but i have a week, and i do not know all the tricks of the trade to get this to work.

    so someone... please help me

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why would we run your code when you posted it before a bunch of things were posted out to you? Furthermore, why should we spend the time to debug your program when you're too lazy. You have a week and that's not enough time for you, but you want me to stop everything I'm doing fix it for you?


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    And the way to get a program to work is to code one function at a time (or even a small part of that one function), test that one function by trying it with various inputs, then once you're convinced it works, move on to another function. Don't just slap it all together, then hope it works as expected.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcat causing segmentation fault?
    By jbsloan in forum C Programming
    Replies: 2
    Last Post: 04-02-2005, 10:42 AM
  2. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  3. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  4. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM
  5. strcat segmentation fault
    By captain-cat in forum C Programming
    Replies: 3
    Last Post: 07-20-2004, 10:29 AM