Oct 9, 2013 at 2:38am UTC
I keep getting a segmentation error when ever I have the following code...
1 2 3 4 5 6 7 8 9 10 11
int main(void ) {
//Section 1
unsigned long val = 12;
std::vector<unsigned long > vval;
for (unsigned long i = 0; i < 100; ++i) {
vval.push_back((unsigned long )0);
}
//section 2
bignum* bn1 = new bignum(val);
delete bn1;
}
relevent constructor:
1 2 3 4 5 6
bignum::bignum ( const unsigned long val ) {
if (this ->num == nullptr ) {
delete this ->num
}
this ->num = new std::vector<unsigned long > ( 1, val );
}
here's the thing, it only segfaults when both sections 1 and 2 are there. It runs fine when section 1 is alone, and when section 2 is alone.
bignum::num is defined as std::vector<unsigned long>*
Error: *** Error in `/home/alex/projects/bignum/build/bignum': free(): invalid pointer: 0x00007ffff75b5b88 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x80a46)[0x7ffff7274a46]
compiler is clang++ 3.2
edit:
It doesn't happen if I restructure it so that bignum::num is not a pointer to an std::vector<unsigned long>
Last edited on Oct 9, 2013 at 3:28am UTC
Oct 9, 2013 at 3:51am UTC
That looks identical to what I have, but still aborts. Potential bug?
edit: tried it with g++, should definitely not be a bug.
edit edit: ultimately gave up and left num a non pointer.
Last edited on Oct 9, 2013 at 3:57am UTC
Oct 9, 2013 at 4:09am UTC
tried it with g++, should definitely not be a bug.
no comprende. Did it work with g++?
If you c/p the code in my post does it compile?
EDIT: Whoops. Missed your edit edit.
Last edited on Oct 9, 2013 at 4:13am UTC
Oct 9, 2013 at 4:16am UTC
Good eye naraku9333. Missed that.
Oct 9, 2013 at 4:25am UTC
But delete is just as happy with a null pointer as with a valid pointer - it just doesn't do anything if the pointer is null.
Oct 9, 2013 at 4:27am UTC
Hurr. Durr. Simple errors, naraku, story of my life.
I've been programming in declarative languages for the last 2 years, trying to get back into C++ and a bloated bignum library is usually how I get into languages.
Oct 9, 2013 at 6:36am UTC
You should add destructor in bignum to free num.
Oct 9, 2013 at 6:36am UTC
It's there. Originally I just called it.