MESSAGE
DATE | 2016-11-29 |
FROM | Ruben Safir
|
SUBJECT | Re: [Hangout-NYLXS] [Learn] Look at this exciting output by my test
|
On 11/29/2016 06:00 PM, Christopher League wrote: > > First thing I try to do is to reduce the program to the SMALLEST program > that reproduces the same error. You can think of it as binary search: > delete (something like) HALF the program. If error is not reproduced, > restore that half and delete the OTHER HALF. If error is reproduced, > then delete half AGAIN. Continue until it's as small as possible. Here's > the minimal crashing program I came up with: > > int main ( int argc, char *argv[] ) > { > std::vector test {0}; > assert(test.size() == 1); // VECTOR HAS JUST ONE ELEMENT! > for(int i = 0; i<10; i++ ){ > test[i] = i; // THEREFORE THIS IS AN ERROR WHEN i>0 > } > return 0; > } > > It exhibits the same error yours does: "free(): invalid next size > (fast)". I added the `assert` to test my assumption about what the `{0}` > initializer means. > > I tried going even further by unrolling and 'sampling' the loop: > > int main ( int argc, char *argv[] ) > { > std::vector test {0}; > assert(test.size() == 1); // VECTOR HAS JUST ONE ELEMENT! > test[0] = 0; // SAFE > test[1] = 1; // ERR > test[9] = 9; // ERR > return 0; > } > > But this -- unfortunately, IMO -- does not crash. Nevertheless, It's > still erroneous. It's ABSOLUTELY NOT ALLOWED to read or write `test[1]` > or `test[9]` because `test.size() == 1`. > > Anyway, the `vector` type HAS a dereference operation that does > bounds-checking. You'll be much better off if you USE IT. That would > look like this: > > int main ( int argc, char *argv[] ) > { > std::vector test {0}; > assert(test.size() == 1); // VECTOR HAS JUST ONE ELEMENT! > test.at(0) = 0; // SAFE > test.at(1) = 1; // CRASH! > test.at(9) = 9; // UNREACHABLE > return 0; > } > > This time it crashes with a much more helpful error message: > > terminate called after throwing an instance of 'std::out_of_range' > what(): vector::_M_range_check: __n (which is 1) >= this->size() (which is 1) > > and if you back-trace in `gdb` it pinpoints exactly where the error is. > (Unlike with your initial error, when it actually crashes AFTER the > actual erroneous access.) > > Due to the bounds-checking, you haven't overwritten any of the memory > management record-keeping, so it can give you honest results. > > If you don't like bounds-checking, TOUGH... do it anyway. ONCE THE > PROGRAM IS WORKING, you can optimize it away where you really think you > need to. (Usually you don't.) > > That's my $0.03. > > CL >
maybe this http://rsos.royalsocietypublishing.org/content/3/3/150636
not what I was thinking of but a good article
-- So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998 http://www.mrbrklyn.com
DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002 http://www.nylxs.com - Leadership Development in Free Software http://www2.mrbrklyn.com/resources - Unpublished Archive http://www.coinhangout.com - coins! http://www.brooklyn-living.com
Being so tracked is for FARM ANIMALS and and extermination camps, but incompatible with living as a free human being. -RI Safir 2013 _______________________________________________ hangout mailing list hangout-at-nylxs.com http://www.nylxs.com/
|
|