Thread: Segmentation fault: 11

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    2

    Segmentation fault: 11

    Hi, I'm trying to make a program that writes words to a text. The words are generated by a brute force program. I am getting a segmentation fault 11 but i don't know where i went wrong. Please help! Thanks!

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    static const char alphabet[] =
            "abcdefghijklmnopqrstuvwxyz"
            "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
            "1234567890";
    
    
    static const int alphabet_size = sizeof(alphabet) - 1;
    
    
    void brute_impl(char * str, int index, int max_depth)
    {
        int i;
        int sys;
        FILE *fp;
        fp=fopen("~/Desktop/passlist.txt", "w+");
        for (i = 0; i < alphabet_size; ++i)
        {
            str[index] = alphabet[i];
    
    
            if(index == max_depth-1)
            {
                fprintf(fp, "%s\n", str);
                fclose(fp);
            }
    
    
            else
            {
                brute_impl(str, index + 1, max_depth);
            }
        }
    }
    
    void brute_sequential(int max_len)
    {
        char * buf = malloc(max_len + 1);
        int i;
    
        for (i = 1; i <= max_len; ++i)
        {
            memset(buf, 0, max_len + 1);
            brute_impl(buf, 0, i);
        }
    
    
        free(buf);
    }
    
    
    int main(void)
    {
        brute_sequential(2);
        return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Once you close a file, it's a bit silly to keep trying to write to it. (That is: don't close your file until you've finished writing.)

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    It's incorrect to open a file for w+ when you are not planning to read from it (w+ means writing and reading, truncating (or creating) the file first). It's possible that you meant a (for "append"), although it would be more efficient to open the file for writing at the start of brute_sequential, closing it at the end, and pass the file pointer to brute_impl.

    And you're not ending the recursion properly. You should test for your recursion base case before the alphabet loop, not inside it. I believe the test should be index == max_depth (not max_depth - 1). And it's probably best to explicitly zero-terminate the string before writing it instead of relying on the memset in brute_sequential.

    Also, beware the combinatorial explosion! If you get your program working properly and run it with an argument of 8 instead of 2, it will (attempt to) generate a file of over 200,000 gigabytes.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Nov 2013
    Posts
    2
    Quote Originally Posted by oogabooga View Post
    It's incorrect to open a file for w+ when you are not planning to read from it (w+ means writing and reading, truncating (or creating) the file first). It's possible that you meant a (for "append"), although it would be more efficient to open the file for writing at the start of brute_sequential, closing it at the end, and pass the file pointer to brute_impl.
    How would this look like in the code?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Quote Originally Posted by pilsang0512 View Post
    How would this look like in the code?
    That's a dead giveaway that you didn't write the original code to begin with.
    c - Brute Force Password Cracking Algorithm - Stack Overflow

    Two small crumbs to see if you're actually interested in learning, or just another freeloader looking for someone else to finish off what you found on the web to start with.

    You pass fp in the same way you pass 2 (for example).

    And ~ is a shell metacharacter for the home directory, fopen() won't understand it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation fault
    By nils8950 in forum C Programming
    Replies: 20
    Last Post: 12-20-2012, 01:27 PM
  2. Segmentation Fault
    By bigparker in forum C Programming
    Replies: 14
    Last Post: 07-14-2009, 06:55 PM
  3. Help on Segmentation Fault!!!
    By SweeLeen in forum Linux Programming
    Replies: 4
    Last Post: 02-19-2006, 11:33 AM
  4. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM