Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

Sign up now!

C Program need explanation

Joined
Jul 16, 2015
Messages
212
This is a program which combines all possible letter+number combinations to a set length of characters. I sort of understand what is going on but the 'bruteSequential' function confuses me...don't quite understand that. Was wondering if someone could explain

Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


static const char alphabet[] =
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789";

static const int alphabetSize = sizeof(alphabet) - 1;

void bruteImpl(char* str, int index, int maxDepth)
{
    for (int i = 0; i < alphabetSize; ++i){
        str[index] = alphabet[i];

        if (index == maxDepth - 1) printf("%s\n", str);
        else bruteImpl(str, index + 1, maxDepth);
    }
}

void bruteSequential(int maxLen)
{
    char* buf = malloc(maxLen + 1);

    for (int i = 1; i <= maxLen; ++i){
        memset(buf, 0, maxLen + 1);
        bruteImpl(buf, 0, i);
    }

    free(buf);
}

int main(void)
{
    int x;
    printf("Length: \n");
    scanf("%x",&x);
    bruteSequential(x);

    return 0;
}
 
Joined
Dec 19, 2014
Messages
1
malloc() is a memory allocation function that accepts the number of bytes to allocate as the parameter.
memset() is a function to store data at a specific address; 'buf' being the destination, '0' being the data to store, and maxLen+1 being the size of the data to store. The purpose of this is to prevent dirty memory, which is when you have previously stored data in a pointer.
Then it stores the possible combinations of characters at this location.
 
Darn Hunk
Joined
Mar 16, 2015
Messages
11
To add onto what @Coma said, there are a few things here that should have been clarified by the original writer.

The reason the memory allocation is maxLen + 1 is because strings in C need to be null-terminated. This refers to ending the string with the \0 character. More specifically, this is needed because printf will print up to (but not including) the null-terminator. So if it isn't there, you get undefined behavior.

Consider the following:

Code:
#include <stdio.h>
#include <stdlib.h>

int main()
{
    size_t bufferSize;
    char * buffer;
   
    bufferSize = 5;
    buffer = (char *) malloc(bufferSize);
   
    buffer[0] = 'a';
    buffer[1] = 'b';
    buffer[2] = 'c';
    buffer[3] = 'd';
    buffer[4] = 'e';
   
    printf("%s", buffer);
   
    free(buffer);

    return 0;
}

This is prone to undefined behavior as a null-terminator is never included. The full buffer is allocated with non-zero characters. Now, seeing as this program is so simple it is unlikely to run into issues as most of the memory will be empty. However, proper precautions should always be taken.

---

After that, the loop runs through and clears the buffer. This is where the null-terminator is added, as the entire buffer is initialized to 0, so there will always be at least one \0 present in the buffer (assuming the maximum length is respected).

The actual implementation is then called. Blah blah blah, prints out some permutations.

The buffer is then freed so the allocater can now reuse that memory.

---

By reusing the buffer we can force undefined behavior. You'll notice that some extra character(s) get printed out at the end that shouldn't be printed. This is because I purposefully forgot the null-terminator.

Code:
#include <stdio.h>
#include <stdlib.h>

int main()
{
    size_t bufferSize;
    char * buffer;
   
    bufferSize = 5;
    buffer = (char *) malloc(bufferSize);
   
    buffer[0] = 'a';
    buffer[1] = 'b';
    buffer[2] = 'c';
    buffer[3] = 'd';
    buffer[4] = 'e';
   
    printf("%s\n", buffer);
   
    bufferSize = 3;
    buffer = (char *) malloc(bufferSize);

    buffer[0] = 'z';
    buffer[1] = 'y';

    printf("%s", buffer);

    free(buffer);

    return 0;
}

It gave results such as:

Code:
abcde
zy~

Code:
abcde
zy>

And a few others.
 
Conelander
Joined
Oct 30, 2014
Messages
3,572
malloc() is a memory allocation function that accepts the number of bytes to allocate as the parameter.
memset() is a function to store data at a specific address; 'buf' being the destination, '0' being the data to store, and maxLen+1 being the size of the data to store. The purpose of this is to prevent dirty memory, which is when you have previously stored data in a pointer.
Then it stores the possible combinations of characters at this location.
hey coma!!! im your biggest fan!!!
 
Top