Martfosz
Martfosz

Reputation: 1

Where am i doing a mistake in malloc?

I want to convert number X from 'input' number system to 'output' number system and result is save in out. I am using malloc for first time and I do not know how to use it properly. The program works but it return -1073740940 and I do not get any number and result. This function take number if the output system is the same as input it return the same number. Otherwise it start converting. I do not know where is an error. But when I was using printf("HERE") to locate an error it was tab = malloc(N* sizeof(int)); after this command it doesn't work any more. Fill and FIlli functions fill all array in NULL. You have this functions under the conversion. Please help. I need to end this project so fast. I don't have any time. The N is 4000. It's define. I am using scanf to read this 3 variables than I am argumentx = malloc(N * sizeof(char)); conversion(input, output, x, argumentx); SO NOBODY KNOWS WHATS WRONG :))))))) THANK YOU

void conversion(int input, int output, char* x, char* out)
{  
    int length = strlen(x), which = 0, le, tmp, y, k, first = 0, howmuch = 0, len, len2;
    int *tab, *carry, *end;
    len = length + 3;
    for (int i = 0; i < length; i++)
    {

        if (x[i] >= '0' && x[i] <= '9')
        {
            if (x[i] - '0' >= input)
            {
                bad = 1;
                return;
            }
        }
        if (x[i] >= 'A' && x[i] <= 'Z')
        {
            if (x[i] - 'A' >= input)
            {
                bad = 1;
                return;
            }
        }
        if (x[i] > 'Z')
        {
            bad = 1;
            return;
        }
    }
    if (input == output) {
        for (int i = 0; i < length; i++)
        {
            out[i] = x[i];
        }
        return;
    }
    tab = malloc(N* sizeof(int));
    printf("flaga\n");
    for (int i = length - 1; i >= 0; i--)
    {
        if (x[i] >= '0' && x[i] <= '9')
        {
            tab[which++] = (int)(x[i] - '0');
        }
        else
        {
            tab[which++] = 10 + (int)(x[i] - 'A');
        }
    }
    if (length * (input / (output)) > length * (output / (input)))
    {
        le = length * (input / (output));
    }
    else
    {
        le = length * (output / (input));
    }
    len2 = which * le + 3;
    
    carry = malloc(N * sizeof(int));
    end = malloc(N * sizeof(int));
    filli(len2,carry);
    filli(len2,end);
    carry[0] = 1;
    
    for (int i = 0; i < which; i++)
    {
        for (int j = 0; j < le; j++)
        {
            end[j] += carry[j] * tab[i];
            tmp = end[j];
            y = 0;
            k = j;
            do {
                y = tmp / output;
                end[k] = tmp - y * output;
                k++;
                end[k] += y;
                tmp = end[k];

            } while (tmp >= output);
        }
        for (int j = 0; j < le; j++)
        {
            carry[j] = carry[j] * input;
        }
        for (int j = 0; j < le; j++)
        {
            tmp = carry[j];
            y = 0;
            k = j;
            do {
                y = tmp / output;
                carry[k] = tmp - y * output;
                k++;
                carry[k] += y;
                tmp = carry[k];
            } while (tmp >= output);
        }
    }
    fill(out);
    tmp = 0;
    first = 0;
    for (int i = le; i >= 0; i--)
    {
        if (end[i] != 0)
        {
            first = 1;
        }
        if (first == 0)
        {
            continue;
        }
        if (end[i] < 10)
        {
            out[tmp] = (char)(end[i] + '0');
            tmp++;
        }
        else
        {
            out[tmp] = (char)(end[i] + 'A' - 10);
            tmp++;
        }

    }
    out[tmp] = out[tmp + 1];
    free(carry);
    free(end);
    free(tab);
    return;
}
void fill(char* x)
{
    int length = strlen(x);
    for (int i = 0; i < length; i++)
    {
        x[i] = (char)NULL;
    }
    return;
}
void filli(int length,int* x)
{
    for (int i = 0; i < length; i++)
    {
        x[i] = (int)NULL;
    }
    return;
}

Upvotes: 0

Views: 77

Answers (1)

0___________
0___________

Reputation: 67476

I would split the task into two functions.

static const char digits[] = "0123456789abcdefghijklmnoprqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
unsigned long long fromString(const char *input, const unsigned base)
{
    unsigned long long result = 0;
    if(base > 1 && base <= sizeof(digits))
    {
        while(*input)
        {
            const char *res;
            if(!(res = strchr(digits, *input)) || (res - digits) >= base)
            {
                result = 0;
                break;
            }
            result *= base;
            result += res - digits;
            input++;
        }
    }
    return result;
}

char *toString(unsigned long long val, const unsigned base)
{
    size_t length = 1;
    unsigned long long mask = 1, wrk = val;
    char *result = NULL;

    if(base > 1 && base <= sizeof(digits))
    {
        do {++length; mask *= length > 2 ? base : 1;} while(wrk /= base);
        result = malloc(length);
        if(result)
        {
            char *wrkresult = result;
            do
            {
                *wrkresult++ = digits[val / mask];
                val %= mask;
            }while(mask /= base);
            *wrkresult = 0;
        }
    }
    return result;
}

char *fromAtoB(const char *input, const unsigned inputBase, const unsigned outputBase)
{
    return toString(fromString(input, inputBase), outputBase);
}

int main(void)
{
    printf("%s", fromAtoB("255", 10, 16));
}

Upvotes: 1

Related Questions