-2

I am getting segmentation fault(core dumped) at the time of compilation in my program. I am not able to detect the problem with the program. I am pasting my code. If anyone gets the problem then, please reply.

#include<bits/stdc++.h>
using namespace std;
typedef long double ld;

int main()
{
    int a,b,c;
    cin>>a>>b>>c;
    ld dp[101][101][101];
    for(int i=100;i>=0;i--)
    {
        for(int j=100;j>=0;j--)
        {
            for(int k=100;k>=0;k--)
            {
                if(i==100 || j==100 || k==100)
                {
                    dp[i][j][k] = 0;
                }
                else
                {
                    long double cnt = i+j+k;
                    dp[i][j][k] = 1 + (1.0*i/cnt)*dp[i+1][j][k] + (1.0*j/cnt)*dp[i][j+1][k] + (1.0*k/cnt)*dp[i][j][k+1];
                }
            }
        }
    }  
    cout<<dp[a][b][c];
    return 0;
}
Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62
  • 1
    `1.0*i/cnt` What happens on the last iteration when `i = j = k = 0`? – dxiv Nov 26 '20 at 06:12
  • ohh! I didn't consider it earlier but after seeing your comment I put it in the if-else clause but it is still giving me the same error. – Akhil Sharma Nov 26 '20 at 06:19
  • 3
    Also see why not to [Declare large array on Stack](https://stackoverflow.com/questions/17029671/declare-large-array-on-stack). – dxiv Nov 26 '20 at 06:21
  • 2
    Two suggestions: 1) Please run your app in the debugger, get a stack traceback: https://www.xspdf.com/resolution/51890324.html This is CRITICAL. Please update your post with the traceback. 2) Consider changing `ld dp[101][101][101];` to `new` or [malloc()](https://man7.org/linux/man-pages/man3/malloc.3.html) (dynamic allocation). I suspect the "crash" might simply be [stack overflow](https://whatis.techtarget.com/definition/stack-overflow) – paulsm4 Nov 26 '20 at 06:44
  • 1
    *"I am getting segmentation fault(core dumped) at the time of compilation in my program."* -- doubtful. More likely you are doing some sort of "build & run" operation and you are getting the crash at the time of *running* your program. A debugger or diagnostic messages sent to `std::cerr` should enable you to locate which line triggers the crash. From that, please construct a [mre] that focuses on the problem. (If the crash seems to occur before any lines run, try trimming everything from `main` but the definition of `dp`.) – JaMiT Nov 26 '20 at 06:45
  • 2
    Someone gives an answer pointing out one particular cause, and you edit the question so that their answer is no longer valid. So, apparently, your intent is to keep editing your code each time you get an answer, until it is completely debugged. Using SO as a means of iteratively debugging code in that manner will, at the least, annoy a fair few people. – Peter Nov 26 '20 at 07:38
  • The segmentation fault is probably caused by the large array on the stack: https://wandbox.org/permlink/BVX0ZqSTTHOGQnEr – Thomas Sablik Nov 26 '20 at 08:07

1 Answers1

2

Compilation error and Segmentation fault are entirely different things. When compiler errors, you can't run the code at all. Seg-fault happens after running the code.

There are few places where the program can crash:

  1. When i = j = k = 0, the value of cnt is also 0. Attempt to divide by 0, 1.0 * i / cnt results in crash.

  2. If user enters anyone of the values among a,b,c as negative or more than 100, then the final cout statement will cause array out of bound and is again a good candidate for crash.

iammilind
  • 68,093
  • 33
  • 169
  • 336
  • I have edit the code but it is still giving segmentation fault. It's not taking input so the second point is not the reason. – Akhil Sharma Nov 26 '20 at 06:34
  • 3
    @iammilind - you've given a good response ... but perhaps the most IMPORTANT thing the OP can do is to run his app in a DEBUGGER, and get a viable STACK TRACEBACK. Until then, we really don't know WHY the crash is occurring. – paulsm4 Nov 26 '20 at 06:41