Thread: Memory Problem/Segmentation fault

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    33

    Memory Problem/Segmentation fault

    I'm writing a brute force algorithm to create a string that searches through all combinations of letters and numbers and compares it with another string which I declare myself however when running it always crashes with:

    Program received signal SIGSEGV, Segmentation fault.
    0x00007fff5fc185e4 in ?? ()

    When running it with gdb it always crashes between 'ac8Q' and 'ac8T'. Because it never crashes in the same place it makes me think it isn't a problem with the code but memory. Is there some default limited amount of memory to be used for a program or is this something else?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > When running it with gdb it always crashes between 'ac8Q' and 'ac8T'.
    > Because it never crashes in the same place

    These things are mutually exclusive. If you can observe exactly when it crashes, then it is more or less crashing "in the same place".

    > it makes me think it isn't a problem with the code but memory. Is there some default limited amount of memory to be used for a program or is this something else?

    There is a limited amount of stack memory, but had that been the cause of the problem you would know. There tends to be a more precise output like "smashed stack memory." Segmentation faults are almost always a user error... without the code, it's pretty hard to tell you the exact cause.

    > 0x00007fff5fc185e4 in ?? ()
    Memory locations like this doesn't really tell me anything useful by itself. Someone more familiar with the memory layout might be able to give you an estimated guess, but really the code is missing, so is the answer.


  3. #3
    Registered User
    Join Date
    Feb 2015
    Posts
    33
    The code goes through all combinations of the alphabet array from 'a' to as mentioned previously, approximately 'ac8R', which is why i think it's something to do with memory because it works fine prior to the fault and seems an arbitrary place to break.

    The program works with recursive functions, main() calls the down_alg() function to get it going and if a match isn't found it should return to main and exits, if it is found it exits at the 2nd 'if' statement of the down_alg() function. The purpose of the down_alg function is to increment the last array space, e.g from 'aa' to 'ab'.

    When the final column reaches the end of the alphabet array, AT_END it calls the increment_alg() which increments the columns that aren't in the final column e.g from 'a9' to 'ba'. Or 'Fb9' to 'Fca' and then returns to the down_alg().

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define AT_END alpha[61]
    
    
    void down_alg();
    void increment_alg();
    
    
    //Global
    int column = 1; //determines the column you're in
    int length = 1; //determines the length of the array
    char *pwdarr;
    char *alpha;
    int i = -1; //column is 1
    int j = 0; //column is 2
    int k = 0; // column is 3
    int l = 0;
    int m = 0;
    char *realpwd; //string that program searches for
    int found = 1;
    int funcret = 0;
    
    
    int main()
    {    
        alpha = (char*)malloc(sizeof(char)*63);
        alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        realpwd = (char*)malloc(sizeof(char)*10);
        realpwd = "ac8S";
        pwdarr = (char*)malloc(sizeof(char)*10);
        
        down_alg();
    
    
        printf("Password not found\n");    
        return 0;
    }
    
    
    void down_alg()
    {
        if(funcret == 1) //if just returned from increment_alg then skip else statement
        {
            funcret = 0;
        }
        else
        {
            switch(column)
            {
                case 1:
                    ++i;
                    pwdarr[column-1] = alpha[i];
                    break;
                case 2:
                    ++j;
                    pwdarr[column-1] = alpha[j];
                    break;
                case 3:
                    ++k;
                    pwdarr[column-1] = alpha[k];
                    break;
                case 4:
                    ++l;
                    pwdarr[column-1] = alpha[l];
                    break;
                case 5:
                    ++m;
                    pwdarr[column-1] = alpha[m];
                    break;
            }
        }
        if(strcmp(realpwd, pwdarr) == 0) //if strcmp returns true then pwd is found
        {
            printf("Password is %s\n", pwdarr);
            exit(0);
        }        
        else if(pwdarr[column-1] != AT_END) //if not at end
        {
            down_alg();
        }
        else if(pwdarr[column-1] == AT_END) //if at end
        {
            increment_alg();
        }
    }
    
    
    void increment_alg() //function used to reset column when it is AT_END
    {
        if(column == 1)
        {
            if(pwdarr[0] == AT_END)
            {
                pwdarr[column-1] = alpha[0];    
                int inc = 0;
                length += 1;
                i = 0;
                while(inc < length)
                {    
                    pwdarr[inc] = alpha[0];
                    ++inc;
                }
            }
            column = length;
            funcret = 1;
            down_alg();
        }        
        if(pwdarr[column-1] == AT_END)
        {
            switch(column)
            {
                case 2: //j column
                    j = 0;
                    pwdarr[column-1] = alpha[j];
                    break;
                case 3:
                    k = 0;
                    pwdarr[column-1] = alpha[k];
                    break;
                case 4:
                    l = 0;
                    pwdarr[column-1] = alpha[l];    
                    break;
                case 5:
                    m = 0;
                    pwdarr[column-1] = alpha[m];
                    break;
            }
        }
        if(pwdarr[column-2] == AT_END)
        {
            --column;
            increment_alg();
        }
        else if(pwdarr[column-2] != AT_END) //increments the previous column is not AT_END instead of recalling function
        {
            switch(column-2)
            {
                case 0:
                    if(i == 61)
                        i = 0;
                    ++i;
                    pwdarr[column-2] = alpha[i];
                    break;
                case 1: //column == 2, 'j'
                    if(j == 61)
                        j = 0;
                    ++j;
                    pwdarr[column-2] = alpha[j];
                    break;
                case 2: 
                    if(k == 61)
                        k = 0;
                    ++k;
                    pwdarr[column-2] = alpha[k];
                    break;
                case 3:
                    if(l == 61)
                        l = 0;
                    ++l;
                    pwdarr[column-2] = alpha[l];
                    break;
                case 4:
                    if(m == 61)
                        m = 0;
                    ++m;
                    pwdarr[column-2] = alpha[m];
                    break;
            }
            column = length; 
            funcret = 1;
            down_alg();
        }
    }

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,671
    Couple of things:

    Code:
    alpha = (char*)malloc(sizeof(char)*63);
    alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    realpwd = (char*)malloc(sizeof(char)*10);
    realpwd = "ac8S";
    You are allocating heap memory, then immediately throwing that away by assigning a constant string to each of the variables.

    Code:
    if(strcmp(realpwd, pwdarr) == 0) //if strcmp returns true then pwd is found
    pwdarr is not null-terminated, so attempting to use strcmp on it is undefined behavior.

    Also, way too much reliance on global variables.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    I noticed some glaring problems at the start, so I haven't gone too deeply here.

    Code:
    alpha = (char*)malloc(sizeof(char)*63);
    alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    realpwd = (char*)malloc(sizeof(char)*10);
    realpwd = "ac8S";
    You should not be assigning strings to your pointers in this way. Instead, you should be using a function such as "strcpy()" to copy the string to the char pointer.

    Additionally, you should be using a debugger to troubleshoot your code - especially when you get a seg fault. My debugger pointed out this line:

    Code:
    if(strcmp(realpwd, pwdarr) == 0)
    You assign a single character to "pwdarr" in the above "switch()", so it is not a proper string (null-terminated character array). Hence, you shouldn't be using it with "str" functions.

    A couple of other observations:


  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    In addition to the errors pointed out by others you are probably running out of stack space as you suspected. I calculate that you're recursing at least a quarter million levels. You should redesign your algorithm to eliminate recursion.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault: Cannot access memory at address
    By Joelito in forum Linux Programming
    Replies: 2
    Last Post: 05-15-2014, 07:00 PM
  2. Allocating 4 MB of Memory causes Segmentation Fault
    By TangoOversway in forum C++ Programming
    Replies: 15
    Last Post: 03-15-2013, 11:29 AM
  3. Replies: 7
    Last Post: 11-05-2011, 05:11 PM
  4. Replies: 3
    Last Post: 03-12-2011, 08:28 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM