×

# HELP needed with SIGSEGV using dynamic Segment TREE

 0 I tried the problem www.codechef.com/problems/CBFEAST/ and could only come up with algorithms for 1st and 2nd subtask. For subtask 2 I planned to use segment trees, so I wrote a segment tree code for my own use. The BUILD function gives a SIGSEGV error. I have tried the same TREE on leetcode and SPOJ and did not receive any segmentation fault. For sub2 I build a segment tree representing 10^7 buckets and each bucket represents 100 colors for a total of 10^9 colors. Please help me with this, to make it easy to help me I have figured out the following things : 1. Even if I use a small n = 100 instead of 10^7, I receive SIGSEGV. 2. Build function definitely leads to SIGSEGV, as I tried the code with only build and no other code. 3. However when I used n = 64,I Did not receive SIGSEGV. asked 19 Dec '18, 03:38 3●2 accept rate: 0%

 1 Are you out-of-bounds on the array ar? ar is an array length 10000000, and then I think you access it up to index 16777216? One way to catch this sort of thing is to use vector ar(10000000) in place of ar[10000000]. Instead of accessing ar[x], you would use ar.at(x). Any attempt to access outside the bounds of the array will crash with helpful message. answered 19 Dec '18, 05:41 575●1●7 accept rate: 27% Thanks for the reply and I'll try using vector and report back, however I don't feel this is the problem. For leaf nodes I have assigned a 0 if node number greater than n. Also I have tried the code without even using the array, had the code which used array commented out and only built the tree still got Same error.Thanks for the help will still try vector. (19 Dec '18, 11:15) Yes, you assign 0 if node greater than n. But you have set n to be larger than the size of the ar array.  n=16777216; SegmentTree sg(ar,n);  (19 Dec '18, 12:42) https://www.codechef.com/viewsolution/22008321 check this please even n = 1000 gives SIGSEGV with build function. (19 Dec '18, 12:56) Thankyou, was able to fix my problem using vector.at() turns out problem was not with the tree but with my understanding of the question. (20 Dec '18, 22:05)
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×530
×376
×185
×102
×23
×16

question asked: 19 Dec '18, 03:38

question was seen: 211 times

last updated: 20 Dec '18, 22:05