unexpected SIGSEG ABORT when working with vector

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
The following compiles fine here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <vector>

class bignum{
public:
    bignum(const unsigned long val);
private:
    std::vector<unsigned long> *num;
};

bignum::bignum ( const unsigned long val ) {
    if(this->num == nullptr) {
        delete this->num;
    }
    this->num = new std::vector<unsigned long> (1, val );
}

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;
}

$ clang++ -v
Debian clang version 3.3-9 (branches/release_33) (based on LLVM 3.3)
Target: x86_64-pc-linux-gnu
Thread model: posix
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
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
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 );
}

Shouldn't that be !=
Good eye naraku9333. Missed that.
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.
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.
L B wrote:
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.
True, and since it is in a ctor there's no chance of num being allocated already so the check isn't even necessary.
You should add destructor in bignum to free num.

It's there. Originally I just called it.
Topic archived. No new replies allowed.