Re: vector has segfault null dereference
* andrey.vul@gmail.com:
On Oct 1, 9:31 pm, andrey....@gmail.com wrote:
On Oct 1, 8:01 pm, "Alf P. Steinbach" <al...@start.no> wrote:
...
//get all available options for value val
vector<u8> *options(u16 val) {
vector<u8> *cc = new vector<u8>();
According to my debugging, ^this^ line is throwing std::bad_alloc due
to a null pointer being returned from malloc. What is weird is that I
am allocating only 1 vector and I have 1 GB of ram so why is malloc
returning null?
Call trace:
[OS-dependent stuff]
C++ library!malloc()
C++ library!operator new()
sudoku![std::vector allocate]
sudoku!std::vector::insert
sudoku!std::vector::push_back
sudoku!solver::options
...
sudoku!main
...
u8 i;
//find and add avialbale numbers
for (i = 0; i < 9; ++i)
if (((1 << i) & val) == 0)
cc->push_back(i + 1);
return cc;
}
...
You have an infinite recursion in your search() function.
But the code has many other problems.
I suggest you start instead with removing all gotos, all raw arrays, all
calls to calloc and malloc and so on. One way to do that, which is what
I would prefer, is to copy your existing code to a backup (to look at),
than delete all contents of your file and start from scratch with an
empty main(). Remember to give yourself an electric shock every time
you even /think/ about reaching for goto or raw array or the like... :-)
Cheers, & hth.,
- Alf
PS: Please don't quote signatures.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?