SIGSEV fault fix

WHAT TO LOOK FOR IN A PROGRAM IN ORDER TO FIX THE SIGSEV ERROR ?

1 Like

Its basically the error which is caused by an invalid memory reference or the segmentation fault. A segmentation fault occurs when we are trying to access a memory location we are not allowed to or access it in a way which is not allowed. On X86 system, we have a data segment and stack segment. These two parts of the program’s address space are writable while others are readable(Code segment). Thus attempting to read outside of the program’s address space, or writing to a read-only segment of the address space, results in a segmentation fault, hence the name. Refer this answer by admin to look in for the common issues which raise this fault.

2 Likes

After few tests i found that my program is actually getting
terminate called after throwing an instance of ‘std::bad_alloc’
what(): std::bad_alloc

Can you help me with this please

The exception is usually thrown when the dynamic memory you are trying to allocate is of a large size and OS is unable to allocate it. So, try changing your code’s logic and optimize the space. Or you may like sharing your code, we’ll try helping here :slight_smile:

Ya ofcourse

:slight_smile:

This RTE is because of the fact that the new operator you are using to allocate memory is allocating an array of size n. At the run time you haven’t provided that value of n and thus it is allocating memory of some unspecified size(garbage value of n it is picking). Thus, if you’ll give some input and then check it’ll work fine. Also, you used a macro for MAX and used max as a variable. This causes ambiguity. Also, “max” is a keyword in C++. In some version it’s not allowed to use it. Like in yours, it showed the error that reference to max is ambiguous.

See here, http://ideone.com/AmeWXz I displayed the values of your segment tree and it works absolutely fine.

Link is not opening :confused:

1 Like

Not working for me too, @damn_me is it private or you deleted it?

@betlista and @ravidelcj I’m so sorry, I just forgot to change the visibility. You can now try!!

it’s ok now :wink:

i copied your code and executed it
and it is also giving the same runtime error

@ravidelcj Did you give any input? I can’t see the custom input, Did you read above? I 'll repeat again: You are not given any input and your variables are assigned some random garbage value. Lets say na has -4357 or anything. When your code runs, the tree construction function is called and it starts allocating memory (lines 28-30) which the OS is unable to do. That’s why it is throwing exception. Give input and it’ll work fine!! Read above answer and comments carefully please. :slight_smile:

i am really sorry
i forgot to give the inputs
btw thanks
you once again helped me :slight_smile:

this is a question from spoj GSS1
and its giving SIGSEGV
please help

SIGSEGV means, you are allocating such large memory which is not allowed. And GSS1, this question is not asking what you are doing, its asking which range among the given (i,j) has got the maximum sum. Refer http://e-maxx.ru/algo/segment_tree for that. It has a good explanation of that question. Since, it seems you are just beginning with Segment tree, try solving other basic questions like MTSICK, MARBLEGF etc on codechef to get better understanding of the concept. That’ll really help. Even I started with that GSS1 question months back but then shifted. :slight_smile:

so how are we supposed to handle large inputs in this case

and what do you mean by range among given i j with max sum ?

You are not supposed to handle large inputs, you are supposed to handle the space you are allocating which is large due to large input size. Secondly, the question says which range among the given (i,j) has got the maximum sum. Ex: in range [1,3], the possible ranges are [1,1],[2,2],[3,3], [1,2], [2,3], [1,2,3] question asks which range among all these has maximum sum. SOlution is explained on that russian site i mentioned in the above post.

For further assistance, I think it’s better to have a new thread/forum for that question specifically and closing this after marking the answer accepted. You’ll get more and better answers on that separate thread titled for that question. :slight_smile: Happy to help…