Skip to main content
added 39 characters in body
Source Link
Cherubim
  • 5.5k
  • 3
  • 23
  • 38

I have a problem in the next code. Malloc work malloc works and in the while realloc work-loop, realloc() works for the first buttime and when it is called 2ndthe second time it always fails. The code is part of thean algorithm to get the prime factors of a number.

int main()
{
    int n, in, *ar, len = 0;
    scanf("%d", &n);
    ar = (int *) malloc(1 * sizeof(int));
    while(n % 2 == 0){
        ar[len] = 2;
        len++;
        ar = (int *) realloc(ar, len * sizeof(int));
        if(ar == NULL){
            printf("Error");
            return 1;
        }
        n /= 2;
    }
    return 0;
}

I tried with lenlen initialized to 1 but iit still fails. It is strange it does not fail on the first call but it fails on the 2ndsecond call. I have read other similar questions but iI am a beginerbeginner and iI didn`t understand. Thanks in advance!

I have a problem in the next code. Malloc work and in the while realloc work first but when it is called 2nd time it always fails. The code is part of the algorithm to get the prime factors of a number.

int main()
{
    int n, in, *ar, len = 0;
    scanf("%d", &n);
    ar = (int *) malloc(1 * sizeof(int));
    while(n % 2 == 0){
        ar[len] = 2;
        len++;
        ar = (int *) realloc(ar, len * sizeof(int));
        if(ar == NULL){
            printf("Error");
            return 1;
        }
        n /= 2;
    }
    return 0;
}

I tried with len initialized to 1 but i still fails. It is strange it does not fail on the first call but it fails on the 2nd. I have read other similar questions but i am a beginer and i didn`t understand. Thanks in advance!

I have a problem in the code. malloc works and in the while-loop, realloc() works for the first time and when it is called the second time it always fails. The code is part of an algorithm to get the prime factors of a number.

int main()
{
    int n, in, *ar, len = 0;
    scanf("%d", &n);
    ar = (int *) malloc(1 * sizeof(int));
    while(n % 2 == 0){
        ar[len] = 2;
        len++;
        ar = (int *) realloc(ar, len * sizeof(int));
        if(ar == NULL){
            printf("Error");
            return 1;
        }
        n /= 2;
    }
    return 0;
}

I tried with len initialized to 1 but it still fails. It is strange it does not fail on the first call but it fails on the second call. I have read other similar questions but I am a beginner and I didn`t understand. Thanks in advance!

Source Link
Timʘtei
  • 765
  • 1
  • 8
  • 21

Why does realloc fails every time?

I have a problem in the next code. Malloc work and in the while realloc work first but when it is called 2nd time it always fails. The code is part of the algorithm to get the prime factors of a number.

int main()
{
    int n, in, *ar, len = 0;
    scanf("%d", &n);
    ar = (int *) malloc(1 * sizeof(int));
    while(n % 2 == 0){
        ar[len] = 2;
        len++;
        ar = (int *) realloc(ar, len * sizeof(int));
        if(ar == NULL){
            printf("Error");
            return 1;
        }
        n /= 2;
    }
    return 0;
}

I tried with len initialized to 1 but i still fails. It is strange it does not fail on the first call but it fails on the 2nd. I have read other similar questions but i am a beginer and i didn`t understand. Thanks in advance!