Error 1
Your constructor performs a memory allocation like so:
int* tab = new int[n+1];
This will put the result in a local variable named tab
, not the member field of the same name. Your constructor then does everything with this local variable and the member field is never initialized.
Therefore your CzynnikiPierwsze
is performed with an uninitialized pointer and causes undefined behavior as soon as it accesses it. To compound your problems, you even try to free this never initialized pointer at the end of that function.
Error 2
Also, there is another error in your commented out code. After initialization your array would contain 0 0 2 3 2 5 2 7 2 3 2
. The CzynnikiPierwsze
function when called with 5
would first push_back
tab[5]
which is 5
. Therefore, m
immediately becomes 1
and the vector is returned. Note that only one push_back
was performed.
You then try (in the code you commented out) to get p[0]
, which is ok and p[1]
, which does not exist and, again, causes undefined behaviour - or an exception if you are lucky.