Boston Linux & Unix (BLU) Home | Calendar | Mail Lists | List Archives | Desktop SIG | Hardware Hacking SIG
Wiki | Flickr | PicasaWeb | Video | Maps & Directions | Installfests | Keysignings
Linux Cafe | Meeting Notes | Blog | Linux Links | Bling | About BLU

BLU Discuss list archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Segmentation fault



First, whenever free() crashes it is generally because the current or a 
previous freed block has been stepped on. Every malloc is a bit different, 
but when you get a malloc block, it normally contains a header containing a 
couple of pointers (for returning it to the freelist) and a size, and maybe 
a few more things. The pointer that is returned generally points to the 
first byte following that. You also generally get more than you asked for 
because the blocks are guaranteed to start on a "natural" boundary (32 bits 
on a 32 bit machine, 64 on a 64 bit machine, or even 128 bits).
First, you do not request the correct len. In your case len == strlen. You 
should add 1 to allow for the trailing null termination.
>     temp = (char *)malloc( len * sizeof(char));

Secondly, in neather case do you check if temp is NULL. I would put a check 
or an assertion in here. Remember that you are calling this recursively. 
Your stack should be ok.
eg; assert(temp);

Another thing I don't like is your rest_of_string function. Look at the 
case when i == (len - 1); Remember that string[i] would point to the end of 
the original string, so what you are doing is:
string[len - 1] = '\0';
strcat(string, string + len);
Since string[len] contains garbage, you are possibly coipying beyond the 
end of the allocated block into another block's header.



Derek Martin wrote:

> #include <stdio.h>
> #include <string.h>
> #include <stdlib.h>
> #include "permute.h"
> 
> int
> permutation( char *string, char *done )
> {
> 
>   int i, j, len;
>   char *temp; 
>   char *done_so_far;
>   char charcurrent[2];
> 
>   len = strlen( string );
>   charcurrent[1] = '\0';
> 
>   if ( len == 1 ){
>     printf( "%s%s\n", done, string );
>     return 0;
>   }
> 
>   else {
> 
>     /* store the string without the current char in temp */    
>     temp = (char *)malloc( len * sizeof(char));
>     done_so_far = (char *)malloc( strlen( done ) + 2);
>    
>     for ( i = 0; i < len; i++ ) {
>       strcpy( done_so_far, done );
>       strcpy( temp, string );
>       temp = rest_of_string( temp, i );
>       charcurrent[0] = string[i];
>       strcat( done_so_far, charcurrent );
> 
>       /* do the recursive call with the placeholder */
>       permutation( temp, done_so_far );
>     }
> 
>     /* forgetting to do this causes a HUGE memory leak */
>     /* as the size of the string(s) grows              */
>     free(temp);
>     free(done_so_far);
>     
>     return 0;
>   }
>   
> }
> 
> 
> char *
> rest_of_string( char *string, int usedchar )
> {
>   
>   string[usedchar] = '\0';
>   strcat( string, (string + usedchar + 1) );
>   return string;
>   
> }
> 
> 
> heeellp meeee... <as I fall down an endless well of frustration>
> 
> -- 
> "Quis custodiet ipsos custodes?"    "Who watches the watchmen?" 
> -Juvenal, Satires, VI, 347 
> 
> Derek D. Martin      |  Senior UNIX Systems/Network Administrator
> Arris Interactive    |  A Nortel Company
> derekm at mediaone.net  |  dmartin at ne.arris-i.com
> -------------------------------------------------
> 
> -
> Subcription/unsubscription/info requests: send e-mail with
> "subscribe", "unsubscribe", or "info" on the first line of the
> message body to discuss-request at blu.org (Subject line is ignored).
> 

-- 
Jerry Feldman <gaf at blu.org>
Boston Linux and Unix user group
http://www.blu.org


-
Subcription/unsubscription/info requests: send e-mail with
"subscribe", "unsubscribe", or "info" on the first line of the
message body to discuss-request at blu.org (Subject line is ignored).




BLU is a member of BostonUserGroups
BLU is a member of BostonUserGroups
We also thank MIT for the use of their facilities.

Valid HTML 4.01! Valid CSS!



Boston Linux & Unix / webmaster@blu.org