C program: memory leak/ segmentation fault/ memory limit exceeded
Hi all,
thank you for all constructive feedback. Sorry about "annoying" thing. Haven't remarked it was still there before posting. This was a joke from the university, as our professor told us we could write both input[n]=x or n[input]=x but the latter was annoying :). I have replaced "realloc" with "malloc", so this piece works (I hope). But now I see the other line where error appears and it looks like the real issue. [CODE]long int * frequency = (long int*) malloc(2 * MAX * MAX * sizeof(long int));[CODE] This is the part where I need to go through my array (where I've saved sums of all pairs on the previous step) and find which numbers/sums are repeating and how many times. I've done it easy but ineffectively and need to change. As of now, I check an array item, and if it contains the number X, I add +1 to the item X in a frequency array, eg frequency[x]+=1. I've read some articles on Internet and it looks like dictionary can be a better structure to use for this purpose. (I need to check every item in an array and create a list of unique numbers/sums that are present in this array; each number/sum to be associated with the frequency). Do you think dictionary will be a good choice for that? I basically need to store pairs int->int, specifically sum(a number stored in an array item) -> frequency (how many times this sum was contained in different array items). 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 [updated] --------- Code:
#include <stdio.h> |
I've written blogs about how to debug core dumps and use the debugger.
Please see the link in my signature. |
Quote:
|
you need to check the return value of malloc, realloc and in general any function. Just to know if they failed or not.
you can use valgrind to check memory related issues you did not provide enough info, for example what did you exactly entered/tried when it failed. you need also explain how did you compile/build this code (including the versions and also the OS). Oh yes, what is this good for? Code:
n [ input ] = x; |
Quote:
|
Quote:
(see also https://c-faq.com/aryptr/joke.html) |
Code:
if (n >= max) { Just to be sure, these checks could be added: Code:
if (n > max) exit (60); |
Quote:
|
Quote:
|
Quote:
|
Quote:
I've used valgrind, it doesn't find anything. First I compiled the code with g++ v11.3.0 in Ubuntu, no error. When I tried uploading my code to the university environment, the error appeared. I've tested then this code in Vs Code and then Visual Studio in Windows and got error messages with more info about where the problem could be. I've tried with 2 examples from below, both failed with the same error Example program output: Input sequence: 1 5 2 4 Total pairs: 1 Input sequence: 1 5 2 4 2 2 2 Total pairs: 12 |
Quote:
I've written about it in the updated text of my question. |
At that point, realloc is perfectly valid, as it keeps the already filled values. It's the later malloc-s where 'n' should be used instead of 'MAX'
Anyways, I tried the latest state of your program with valgrind; it showed that 'frequency' is uninitialized: 'malloc' allocates memory, but doesn't clear it; 'calloc' does that: Code:
- long int * frequency = (long int*) malloc(2 * MAX * MAX * sizeof(long int)); |
Quote:
Frequently, the action of describing a problem and considering common questions is enough to resolve an issue, but even when it's not it means you've done some basic diagnostic steps that allow you to rule certain things out before taking the issue to someone who isn't a duck. |
A note: I'd start with ensuring that all elements are positive: if the minimum of all elements is not positive, then add 1-minimum to every elements.
After that we have n-1 series of nested intervals (only the sum of the elements is relevant; as the elements are positive, these sum-series are increasing). Code:
input: 1 2 4 3 5 7 1 (n=7) |
The points people are trying to drive home are:
|
Quote:
|
Off: now I think my idea to make elements positive by adding a constant value was wrong... So right now I think we have to calculate the n(n-1)/2 interval-sums, then perform a sort to find the duplications.
|
I find the exercise a little conceptually odd, and I don't fully get this concept of "pairs" rather than just reporting the number of duplicate results, but my first run using the input from the example gives the same results as described in OPs description, so I assume I've understood the goal correctly.
Code:
$ time -p (echo "1 5 2 4 2 2 2" | ./sum_range ) Code:
$ time -p ( seq 1 100| ./sum_range | grep Total ) |
Quote:
|
Quote:
|
Yeah, but how many of those are ducks? :)
Anyway, not clear on the current status of the thread... do you still have an outstanding memory leak, or a different issue, or is it solved now? |
All times are GMT -5. The time now is 10:24 PM. |