Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: split in c

  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    split in c

    I wanted a function to split sentences into words so I wrote my own but it doesn't like when i call free() on the 'sentence' variable. Anyone know why?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /**
     * function prototypes
     */
    
    int split(char *input_string, char delim, char ***output_strings, int max_word_size);
    int get_string_length ( char * string );
    
    /**
     * function: split
     * ~~~~~~~~~~~~~~~
     * this function splits a sentence into words based on a delimiter
     * a pointer to an array of char pointers stores the output words
     * param input_string: this is the input sentence to split into words
     * param delim: the delimiter used to split the sentence
     * param output_strings: store the output words found in sentence
     * param max_word_size: specifies the maximum word size expected, no output words will be longer than this
     * returns: the number of words found in the sentence
     */
    
    int split(char *input_string, char delim, char ***output_strings, int max_word_size)
    {
    	int input_string_length = get_string_length(input_string);
    	int split_count = 0;
    	char *tmpstr;
    	int i,j;
    	printf("%s[%d]: input string = %s\n", __FILE__, __LINE__, input_string);
    	for(i = 0; i < input_string_length; i++)
    	{
    		printf("%s[%d]: input_string[%d] = %c\n", __FILE__, __LINE__, i, input_string[i]);
    		if(input_string[i] != delim)
    		{
    			printf("%s[%d]: '%c' found at input_string[%d]\n", __FILE__, __LINE__, input_string[i], i);
    			j = 0;
    			tmpstr = malloc(sizeof(char) * max_word_size);
    			while(input_string[i] != delim && i < input_string_length)
    			{
    				printf("%s[%d]: copying input_string[%d]=%c into tmpstr[%d]\n", __FILE__, __LINE__, i, input_string[i], j);
    				tmpstr[j] = input_string[i];
    				i++;
    				j++;
    				if(j > max_word_size) break;
    			}
    			tmpstr[j] = '\0';
    			printf("%s[%d]: copying tmpstr=%s into (*output_strings)[%d]\n", __FILE__, __LINE__, tmpstr, split_count);
    			(*output_strings)[split_count] = tmpstr;
    			printf("%s[%d]: (*output_strings)[%d]=%s\n", __FILE__, __LINE__, split_count, (*output_strings)[split_count]);
    			split_count++;
    		}
    	}
    	return split_count;
    }
    
    /**
     * function: get_folder_path
     * ~~~~~~~~~~~~~~~~~~~~~~~~~
     * gets a string's length
     */
    
    int get_string_length ( char * string )
    {
    	int i = 0;
    	while ( string [ i ] != '\0' )
    	{
    		i ++;
    	}
    	return i;
    }
    
    /**
     * function: main
     * ~~~~~~~~~~~~~~
     * test the split function
     */
    
    int main(int argc, char *argv[]){
    	char *sentence = "hello penguins world desert justice";
    	printf("%s[%d]:\n", __FILE__, __LINE__);
    	char **words = malloc(sizeof(char*) * 20);
    	printf("%s[%d]:\n", __FILE__, __LINE__);
    	int word_count = split(sentence, ' ', &words, 20);
    	printf("%s[%d]: word_count = %d\n", __FILE__, __LINE__, word_count);
    	int i;
    	for(i = 0; i < word_count; i++)
    	{
    		printf("words[%d]:%s\n", i, words[i]);
    	}
    	printf("%s[%d]: sentence = %s\n", __FILE__, __LINE__, sentence);
    	printf("%s[%d]: &sentence = %p\n", __FILE__, __LINE__, sentence);
    	for(i = 0; i < word_count; i++)
    	{
    		free(words[i]);
    	}
    	free(words);
    	free(sentence);
    	return 0;
    }
    Below is part of the output:

    Code:
    ...
    split.c[90]: sentence = hello penguins world desert justice
    split.c[91]: &sentence = 0x8048970
    *** glibc detected *** ./split: munmap_chunk(): invalid pointer: 0x08048970 ***
    ======= Backtrace: =========
    /lib/tls/i686/cmov/libc.so.6[0xb7e86604]
    ./split[0x8048798]
    /lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe5)[0xb7e2d775]
    ./split[0x8048391]
    ======= Memory map: ========
    08048000-08049000 r-xp 00000000 08:01 140870     /home/chris/test code/split
    08049000-0804a000 r-xp 00000000 08:01 140870     /home/chris/test code/split
    0804a000-0804b000 rwxp 00001000 08:01 140870     /home/chris/test code/split
    081a0000-081c1000 rwxp 081a0000 00:00 0          [heap]
    b7e16000-b7e17000 rwxp b7e16000 00:00 0 
    b7e17000-b7f73000 r-xp 00000000 08:01 156460     /lib/tls/i686/cmov/libc-2.9.so
    b7f73000-b7f74000 ---p 0015c000 08:01 156460     /lib/tls/i686/cmov/libc-2.9.so
    b7f74000-b7f76000 r-xp 0015c000 08:01 156460     /lib/tls/i686/cmov/libc-2.9.so
    b7f76000-b7f77000 rwxp 0015e000 08:01 156460     /lib/tls/i686/cmov/libc-2.9.so
    b7f77000-b7f7a000 rwxp b7f77000 00:00 0 
    b7f7d000-b7f8a000 r-xp 00000000 08:01 139603     /lib/libgcc_s.so.1
    b7f8a000-b7f8b000 r-xp 0000c000 08:01 139603     /lib/libgcc_s.so.1
    b7f8b000-b7f8c000 rwxp 0Aborted

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    why you use free if you didn't alocate memory, for 'sentence'?

    best regards

  • #3
    New to the CF scene
    Join Date
    May 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh, I thought

    Code:
    char *sentence = "hello penguins world desert justice";
    automatically allocates memory, I guess it doesn't do it the same way as malloc() then.


  •  

    Posting Permissions

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