1

I 'm writing a large number plus function, using arrays

#define MAXLEN 10

int * plus(int *a, int *b) {
    int *result;
    int tmp;
    int add = 0;
    result = (int *)malloc(MAXLEN); // A breakpoint exception was triggered
    for (int i = MAXLEN - 1; i >= 0; i--) {
        tmp = a[i] + b[i] + add;
        add = 0;
        if (tmp / 10) {
            add = tmp / 10;
            tmp = tmp % 10;
        }
        result[i] = tmp;
    }
    return result;
}

There's always exception on the malloc line, but in other functions the malloc() works well.

How does it happened?

Here are all the codes:


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

#define MAXLEN 10

int * plus(int *a, int *b) {
    int *result;
    int tmp;
    int add = 0;
    result = (int *)malloc(MAXLEN);
    for (int i = MAXLEN - 1; i >= 0; i--) {
        tmp = a[i] + b[i] + add;
        add = 0;
        if (tmp / 10) {
            add = tmp / 10;
            tmp = tmp % 10;
        }
        result[i] = tmp;
    }
    return result;
}

void deliver(int *a, int *b) {
    for (int i = 0; i < MAXLEN; i++) {
        a[i] = b[i];
    }
}

void init_int(int *a) {
    for (int i = 0; i < MAXLEN; i++) {
        a[i] = 0;
    }
}

int * numWays(int n) {
    int *a, *b, *c, *sum;
    int j;
    a = (int *)malloc(MAXLEN);
    b = (int *)malloc(MAXLEN);
    c = (int *)malloc(MAXLEN);
    init_int(a);
    init_int(b);
    init_int(c);
    init_int(sum);
    a[MAXLEN - 1] = 1;
    b[MAXLEN - 1] = 1;
    c[MAXLEN - 1] = 2;
    for (int i = 0; i < n; i++) {
        sum = plus(plus(a, b), c);

        deliver(a, b);
        deliver(b, c);
        deliver(c, sum);
    }
    return a;
}

int main()
{
    int print_flag = 0;
    int *num;
    num = (int *)malloc(MAXLEN);
    num = numWays(4);
    for (int i = 0; i < MAXLEN; i++) {
        if (num[i]) {
            print_flag = 1;
        }
        if (print_flag) {
            printf("%d", num[i]);
        }
    }
    printf("\n");
    return 0;
}

It's a question about how many ways can a frog jump onto n steps if it can jump 1, 2 or three steps each time, and I want the exact number.

ojipadeson
  • 129
  • 1
  • 9
  • 4
    Try: `malloc(MAXLEN * sizeof(int))`. – kiner_shah Jan 11 '22 at 11:22
  • 2
    I doubt there is really an exception triggered at the line holding `malloc`. How would that fail unless you completely messed up your heap? Instead I assume your error happens in the line where you access the memory: `result[i] = tmp;` – Gerhardh Jan 11 '22 at 11:28
  • 3
    Not related to your problem, but in C you don't need (and should not) cast the return value of `malloc`. And you should check the return value if it is `NULL`. – Gerhardh Jan 11 '22 at 11:29
  • 1
    Generally speaking, you should very often see a `sizeof` in the malloc argument. If there's not a `sizeof` nearby, this is not necessarily wrong, but you should be a little bit suspicious. – Cheatah Jan 11 '22 at 11:56

1 Answers1

3

Change:

result = (int *)malloc(MAXLEN);

to this:

result = malloc(MAXLEN * sizeof(int))

since you want to dynamically allocate memory equal to size of an int, times MAXLEN.

There's always exception on the malloc line..

You mean a compilation error. An exception is a more advanced notion which you can learn later.

PS: Do I cast the result of malloc? No.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • Thank you, but it's another Exception 'Cannot assign a value of type "void *" to an entity of type "int *"' – ojipadeson Jan 11 '22 at 11:32
  • 5
    @ojipadeson It's because you're using a C++ compiler. Compile C code with a C compiler, not with a C++ compiler. And BTW we're not talking about _exceptions_ but about _compilation errors_. – Jabberwocky Jan 11 '22 at 11:34
  • Just for completeness, another option would be to use `result = malloc(sizeof *result * MAXLEN);`. – Cheatah Jan 11 '22 at 13:03