LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C program: memory leak/ segmentation fault/ memory limit exceeded (https://www.linuxquestions.org/questions/programming-9/c-program-memory-leak-segmentation-fault-memory-limit-exceeded-4175718711/)

rtmistler 11-13-2022 08:07 AM

The points people are trying to drive home are:
  • Debug techniques are important to learn, there are multiple methods and they each benefit you. The old printf() can only take you so far.
  • Actually read the code you've written. Consider that you may possibly be making assumptions about how certain sections and library calls work. Review your code to determine if you fully understand every line you wrote and every return from library calls as well as have called those functions properly.
  • There are several sources to check and confirm your interim work: valgrind, clang, compiler warnings and errors and increased scrutiny by optional compilation flags, and the ability to run, freeze execution, and examine variables.

dugan 11-13-2022 10:58 AM

Quote:

Originally Posted by AlinaC (Post 6392034)
I didn't get what you intended to say with this phrase. Was it intentionally rude - or you just was asking to explain what this line of code was intended to do?

https://en.wikipedia.org/wiki/Rubber_duck_debugging

NevemTeve 11-13-2022 11:22 AM

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.

GazL 11-13-2022 01:37 PM

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 )
Input items: 7
Range:  0..1: sum = 6
Paired: 0..1 ~ 2..3: sum = 6
Paired: 0..1 ~ 3..4: sum = 6
Paired: 0..1 ~ 4..6: sum = 6
Range:  0..2: sum = 8
Paired: 0..2 ~ 2..4: sum = 8
Paired: 0..2 ~ 3..5: sum = 8
Range:  0..3: sum = 12
Paired: 0..3 ~ 2..6: sum = 12
Range:  0..4: sum = 14
Range:  0..5: sum = 16
Range:  0..6: sum = 18
Range:  1..2: sum = 7
Range:  1..3: sum = 11
Range:  1..4: sum = 13
Range:  1..5: sum = 15
Range:  1..6: sum = 17
Range:  2..3: sum = 6
Paired: 2..3 ~ 3..4: sum = 6
Paired: 2..3 ~ 4..6: sum = 6
Range:  2..4: sum = 8
Paired: 2..4 ~ 3..5: sum = 8
Range:  2..5: sum = 10
Paired: 2..5 ~ 3..6: sum = 10
Range:  2..6: sum = 12
Range:  3..4: sum = 6
Paired: 3..4 ~ 4..6: sum = 6
Range:  3..5: sum = 8
Range:  3..6: sum = 10
Range:  4..5: sum = 4
Paired: 4..5 ~ 5..6: sum = 4
Range:  4..6: sum = 6
Range:  5..6: sum = 4
Total Ranges: 21
Total Paired: 12
real 0.00
user 0.00
sys 0.00

However, my simple solution doesn't scale well:
Code:

$ time -p ( seq 1 100| ./sum_range | grep Total )
Total Ranges: 4950
Total Paired: 4342
real 0.02
user 0.02
sys 0.00
$ time -p ( seq 1 200| ./sum_range | grep Total )
Total Ranges: 19900
Total Paired: 21302
real 0.24
user 0.24
sys 0.00
$ time -p ( seq 1 400| ./sum_range | grep Total )
Total Ranges: 79800
Total Paired: 100903
real 3.50
user 3.49
sys 0.01
$


dugan 11-13-2022 06:09 PM

Quote:

Originally Posted by AlinaC (Post 6391863)
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).

Yes :) Although you'd probably call your implementation a hash table (means the same thing).

AlinaC 11-17-2022 02:40 PM

Quote:

Originally Posted by boughtonp (Post 6392046)
It's a reference to a common debugging technique - instead of asking a colleague for help, you first describe the issue to a rubber duck and imagine it responding with questions as that co-worker might, which you then answer.

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.


Thank you for explaining! However before posting a question, I've talked already to myself, my friends from the Uni, and even my mom ;) so this condition was fulfilled )

boughtonp 11-17-2022 05:22 PM


 
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 11:48 PM.