c++ - How to debug segmentation fault? -


it works when, in loop, set every element 0 or entry_count-1. works when set entry_count small, , write hand instead of loop (sorted_order[0] = 0; sorted_order[1] = 1; ... etc).

please not tell me fix code. not using smart pointers or vectors specific reasons. instead focus on question: sort of conditions can cause segfault? thank you.

---- old -----

i trying debug code isn't working on unix machine. gist of code is:

int *sorted_array = (int*)memory; // know block large enough // allocated malloc earlier  (int = 0; < entry_count; ++i){   sorted_array[i] = i; } 

there appears segfault somewhere in loop. switching debug mode, unfortunately, makes segfault stop. using cout debugging found must in loop.

next wanted know how far loop segfault happend added:

std::cout << << '\n'; 

it showed entire range suppose looping on , there no segfault.

with little more experimentation created string stream before loop , write empty string each iteration of loop , there no segfault.

i tried other assorted operations trying figure out going on. tried setting variable j = i; , stuff that, haven't found works.

running valgrind information got on segfault was "general protection fault" , default response 11. mentions there's conditional jump or move depends on uninitialized value(s), looking @ code can't figure out how that's possible.

what can be? out of ideas explore.

this symptoms of invalid memory uses within program.this bit difficult find looking out code snippet side effect of else bad has happened.

however have mentioned in question able attach program using valgrind. reproducible. may want attach program(a.out).

$ valgrind --tool=memcheck --db-attach=yes ./a.out

this way valgrind attach program in debugger when first memory error detected can live debugging(gdb). should best possible way understand , resolve problem.

once able figure out first error, fix , rerun , see other errors getting.this steps should done till no error getting reported valgrind.

however should avoid using raw pointers in modern c++ programs , start using std::vector std::unique_ptr suggested others well.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -