C program: memory leak/ segmentation fault/ memory limit exceeded

Joined
Nov 12, 2022
Messages
1
Reaction score
0
Hi all,

hope you can advise me on the following.
I'm writing a program and get the error message as above.
Debugging has shown that it relates to the following line of code:

C:
input = (int * ) realloc ( input, sizeof ( * input) * max );


Can you please help me to find what is wrong with it and advise how can I fix the bug?
The task itself and full code are below.

Thanks!
Alina

------the task ----------------

The problem is to develop a program to analyze input sequence.

Assume a sequence of numbers. We may choose a contiguous range from the input sequence, for instance, we may choose the range between the 5-th number and the 10-th number. A range must contain at least two numbers. The sum of the numbers may be computed for such range. We choose all possible ranges from the input sequence and we compute the sum for each of them. We are interested how many different ranges exist in the input sequence such that these ranges have the same sum.

For instance, the input sequence may be:

1 5 2 4 2 2 2

there is a total of 21 contiguous ranges of length ≥ 2, in particular:

0..1: 1 5 sum=6
0..2: 1 5 2 sum=8
0..3: 1 5 2 4 sum=12
0..4: 1 5 2 4 2 sum=14
0..5: 1 5 2 4 2 2 sum=16
0..6: 1 5 2 4 2 2 2 sum=18
1..2: 5 2 sum=7
1..3: 5 2 4 sum=11
1..4: 5 2 4 2 sum=13
1..5: 5 2 4 2 2 sum=15
1..6: 5 2 4 2 2 2 sum=17
2..3: 2 4 sum=6
2..4: 2 4 2 sum=8
2..5: 2 4 2 2 sum=10
2..6: 2 4 2 2 2 sum=12
3..4: 4 2 sum=6
3..5: 4 2 2 sum=8
3..6: 4 2 2 2 sum=10
4..5: 2 2 sum=4
4..6: 2 2 2 sum=6
5..6: 2 2 sum=4

Of these 21 ranges, there are 12 pairs such that the ranges in the pair have the same sum:

0..1 ~ 2..3 sum=6
0..1 ~ 3..4 sum=6
0..1 ~ 4..6 sum=6
0..2 ~ 2..4 sum=8
0..2 ~ 3..5 sum=8
0..3 ~ 2..6 sum=12
2..3 ~ 3..4 sum=6
2..3 ~ 4..6 sum=6
2..4 ~ 3..5 sum=8
2..5 ~ 3..6 sum=10
3..4 ~ 4..6 sum=6
4..5 ~ 5..6 sum=4

The input of the program is a sequence of integers (positive, zero, and negative values). The reading of the input sequence is terminated when EOF is reached. The input sequence is at most 2000 numbers long.

The output of the program is the total number of pairs of ranges such that the sums of the ranges are the same.

---------whole code of program ---------

C:
#include <stdio.h>
#include <stdlib.h>
#define MAX 2000


//Take in the list of numbers
//As the list is input, loop within the j-arrays
//Loop through the result & get data into frequency array
//Loop through the frequency array to get the result




int choose(int n, int k){
    if (k == 0) return 1;
    return (n * choose(n - 1, k - 1)) / k;
}




int main(){
   


//***************************
//takes input into the array
int * input = NULL;
int n = 0, x, max = 0;
printf("Input sequence:\n");
while ( scanf ( "%d", & x ) == 1 ) {
        if (n > MAX){
                free ( input );
                printf ( "Invalid input.\n" );
                return EXIT_FAILURE;
            }
        if ( n >= max ) {
            max += max / 2 + 10;
            input = (int * ) realloc ( input, sizeof ( * input) * max );
        }
        //just to annoy people :D
        n [ input ] = x;
        ++n;
    }
if ( ! feof ( stdin ) ) {
        printf ( "Invalid input.\n" );
        free ( input );
        return EXIT_FAILURE;
    }
   
if ( n == 0 ) {
        printf ( "Invalid input.\n" );
        free ( input );
        return EXIT_FAILURE;
    }
   
//****************************
//Finding sums
long int * pointer  = (long int*) malloc(((n-1) * (n-1)) * sizeof(long int));


for (int i = 0; i < n-1; i ++){
    //i is the column now
    //i + 2 is the N of elements in the sum
    for (int j = 0; j< n-1-i; j ++){
        //Add jth, ..., j+i+1 th element
        for (int k = j; k <= j+i+1; k++){
        *(pointer + (n-1)*i + j)  += input[k];
    }
    }
}


free(input);


//*******************************
//getting frequency
long int * frequency  = (long int*) malloc(2 * MAX * MAX  * sizeof(long int));


for (int i = 0; i < n-1; i ++){
    for (int j = 0; j < n-1-i; j ++){
        *(frequency + MAX + *(pointer + (n-1) * i + j)) += 1;
    }}




//*****************************
//Finding the max N of pairs
free(pointer);


int pairs = 0;


for (int i = 0; i < 2 * MAX * MAX; i++){
    if (i[frequency] == 2){
        pairs += 1;
    }
    else if (i[frequency] > 2){
        pairs += choose(i[frequency], 2);
} }










//******************************
//Printing whatever i need to print


free (frequency);


printf("Total pairs: %d\n", pairs);


return 0;}


//Maybe, it just wasn't meant to be




/*
 for (int i = 0; i < n-1; i ++){
    for (int j = 0; j < n-1-i; j ++){
    printf("%ld ", *(pointer + (n-1)*i + j));
    }
    printf("\n");}
 */
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top