bus error, resize vector

I

imutate

I get a bus (or is it buss) error when I resize a vector

typedef std::vector<double> dblvec;
n = 127;
dblvec rv;
rv.resize(n); // error happens here

Any clues ? I can post more info if required ?
 
D

Daniel T.

I get a bus (or is it buss) error when I resize a vector

typedef std::vector<double> dblvec;
n = 127;
dblvec rv;
rv.resize(n); // error happens here

Any clues ? I can post more info if required ?

The below code works perfectly. Try posting the code that actually
produced the error.

#include <vector>

int main()
{
typedef std::vector<double> dblvec;
int n = 127;
dblvec rv;
rv.resize(n); // error happens here
}
 
I

imutate

Daniel said:
The below code works perfectly. Try posting the code that actually
produced the error.

Indeed. How can I do that the whole thing is too large to post, but
why is it relevant. As I said I can post more but why ?

DEBUG_LOG("fn1");
rv.resize(n); // error happens here
DEBUG_LOG("fn2");

I know the error happens at rv.resize(n) because I don't get the output
from the second DEBUG_LOG.
 
R

Roland Pibinger

Indeed. How can I do that the whole thing is too large to post, but
why is it relevant. As I said I can post more but why ?

DEBUG_LOG("fn1");
rv.resize(n); // error happens here
DEBUG_LOG("fn2");

I know the error happens at rv.resize(n) because I don't get the output
from the second DEBUG_LOG.

A bus error usually stems from misaligned objects, not from the code
you posted. Do you use a custom memory allocator?
 
I

imutate

Roland said:
A bus error usually stems from misaligned objects, not from the code
you posted. Do you use a custom memory allocator?

Do you mean like malloc, alloc etc and C++ new ?
No I am not using any of those or third party. I am only using reserve
and resize. Actually I am not using reserve on this particular
variable. I just tried it and it also failed.

DEBUG_LOG("fn0");
rv.reserve(n); // error happens here now
DEBUG_LOG("fn1");
rv.resize(n); DEBUG_LOG("fn2");
 
H

Howard

I get a bus (or is it buss) error when I resize a vector

typedef std::vector<double> dblvec;
n = 127;
dblvec rv;
rv.resize(n); // error happens here

Any clues ? I can post more info if required ?

Probably a bus error. A buss error occurs when you make a mistake kissing
someone. :)

Seriously, the problem may stem from earlier, apparently unrelated code.
Using uninitialized variables or using objects which have been deleted or
writing past the bounds of an array are examples of problems which can often
cause errors to occur later in the code. The code above, by itself, looks
fine.

-Howard
 
I

imutate

Howard said:
Probably a bus error. A buss error occurs when you make a mistake kissing
someone. :)

lol, it is a good word.
http://en.wikipedia.org/wiki/Buss
Seriously, the problem may stem from earlier, apparently unrelated code.
Using uninitialized variables or using objects which have been deleted or
writing past the bounds of an array are examples of problems which can often
cause errors to occur later in the code. The code above, by itself, looks
fine.

-Howard

So as I'm mostly using std vectors, one of these is likely to be the
culprit.
Thanks
 
D

Daniel T.

Daniel T. wrote:

Indeed. How can I do that the whole thing is too large to post, but
why is it relevant. As I said I can post more but why ?

Because if you don't post the code that is actually causing the problem,
then no one can help you. BTW, don't post the whole thing. Start pairing
it down until you have the smallest example possible that still exhibits
the error. Nine times out of ten just trying to do that will help you
find the problem on your own.
DEBUG_LOG("fn1");
rv.resize(n); // error happens here
DEBUG_LOG("fn2");

I know the error happens at rv.resize(n) because I don't get the output
from the second DEBUG_LOG.

resize is not broken. Some code that you wrote is causing the problem.
What type is 'n'?
 
I

imutate

Daniel said:
resize is not broken. Some code that you wrote is causing the problem.

Yes, maybe I need to learn to use some kind of debugger to traceback
the problem.
What type is 'n'?

it is passed in as an int as in
void fn(.. , int n, ..)

I changed this to "const int &n" instead (this code is older code that
I started when I knew less) , but no that did not solve the problem.
 
L

Larry Smith

Yes, maybe I need to learn to use some kind of debugger to traceback
the problem.


it is passed in as an int as in
void fn(.. , int n, ..)

I changed this to "const int &n" instead (this code is older code that
I started when I knew less) , but no that did not solve the problem.


Are you checking that 'n' is not zero, and not negative?
How large might 'n' be?

If you're validating 'n' before using it, then I'd
look elswhere - something is probably trashing memory,
causing this failure as a side effect.
 
I

imutate

Larry said:
Are you checking that 'n' is not zero, and not negative?
How large might 'n' be?

OK its way past my original post
n=127;
If you're validating 'n' before using it, then I'd
look elswhere - something is probably trashing memory,
causing this failure as a side effect.

I looked at all the std::vectors, they all seem to be sized. This
could be tricky, what else tends to do this ? I don't have any
pointers as such except for some function pointer, but since this gets
used lots and I checked it, it is unlikely to be that.
 
D

Daniel T.

Larry Smith wrote:

OK its way past my original post
n=127;


I looked at all the std::vectors, they all seem to be sized. This
could be tricky, what else tends to do this ? I don't have any
pointers as such except for some function pointer, but since this gets
used lots and I checked it, it is unlikely to be that.

Here's a simple thing to do that might find the problem. Replace all
vector array references with the 'at' function. Try this:

First do a find and replace in all files from "vector" (or "std::vector"
if you didn't use any using directives) to "Vec".
Change all references of #include <vector> to #include "Vec.h"
Make a Vec.h file with the following in it:

#include <vector>

// taken from TC++PL 3rd Ed.

template < typename T >
class Vec : public std::vector< T > {
public:
Vec():vector<T>() { }
Vec( int s ) : vector<T>(s) { }
T& operator[](int i) { retur at(i); }
const T& operator[](int i ) const { return at(i); }
};

See if that causes the error to move somewhere closer to the actual
problem.
 
I

imutate

Daniel said:
Here's a simple thing to do that might find the problem. Replace all
vector array references with the 'at' function. Try this:

First do a find and replace in all files from "vector" (or "std::vector"
if you didn't use any using directives) to "Vec".
Change all references of #include <vector> to #include "Vec.h"
Make a Vec.h file with the following in it:

#include <vector>

// taken from TC++PL 3rd Ed.

template < typename T >
class Vec : public std::vector< T > {
public:
Vec():vector<T>() { }
Vec( int s ) : vector<T>(s) { }
T& operator[](int i) { retur at(i); }
const T& operator[](int i ) const { return at(i); }
};

See if that causes the error to move somewhere closer to the actual
problem.

I replaced my matvec.h with this

#include <vector>

template < typename T >
class Vec : public std::vector< T > {
public:
Vec():vector<T>() { }
Vec( int s ) : std::vector<T>(s) { }
T& operator[](int i) { return at(i); }
const T& operator[](int i ) const { return at(i); }
};

typedef Vec<double> vecdbl;
typedef Vec<vecdbl> matdbl;

The compiler went "nuts" threw too many messages to print here, but I
can post them. Is there anything obviously wrong with this template
definition (typo etc) ?
 
G

Greg Comeau

Here's a simple thing to do that might find the problem. Replace all
vector array references with the 'at' function. Try this:

First do a find and replace in all files from "vector" (or "std::vector"
if you didn't use any using directives) to "Vec".
Change all references of #include <vector> to #include "Vec.h"
Make a Vec.h file with the following in it:

#include <vector>

// taken from TC++PL 3rd Ed.

template < typename T >
class Vec : public std::vector< T > {
public:
Vec():vector<T>() { }
Vec( int s ) : vector<T>(s) { }
T& operator[](int i) { retur at(i); }
const T& operator[](int i ) const { return at(i); }
};

See if that causes the error to move somewhere closer to the actual
problem.

I replaced my matvec.h with this

#include <vector>

template < typename T >
class Vec : public std::vector< T > {
public:
Vec():vector<T>() { }
Vec( int s ) : std::vector<T>(s) { }
T& operator[](int i) { return at(i); }
const T& operator[](int i ) const { return at(i); }
};

typedef Vec<double> vecdbl;
typedef Vec<vecdbl> matdbl;

The compiler went "nuts" threw too many messages to print here, but I
can post them. Is there anything obviously wrong with this template
definition (typo etc) ?

If that's the exact code, see:

http://www.comeaucomputing.com/techtalk/#usestd
 
I

imutate

The compiler went "nuts" threw too many messages to print here, but I
can post them. Is there anything obviously wrong with this template
definition (typo etc) ?

OK, different errors now, I will start a new post.
 
D

Daniel T.

Daniel T. wrote:
Here's a simple thing to do that might find the problem. Replace all
vector array references with the 'at' function. Try this:

I replaced my matvec.h with this

#include <vector>

template < typename T >
class Vec : public std::vector< T > {
public:
Vec():vector<T>() { }
Vec( int s ) : std::vector<T>(s) { }
T& operator[](int i) { return at(i); }
const T& operator[](int i ) const { return at(i); }
};

typedef Vec<double> vecdbl;
typedef Vec<vecdbl> matdbl;

The compiler went "nuts" threw too many messages to print here, but I
can post them. Is there anything obviously wrong with this template
definition (typo etc) ?

Just post the first three errors to us and let's see what they say.
 
I

imutate

Daniel said:
Just post the first three errors to us and let's see what they say.

Sorry, I removed this template declaration on advice from someone else,
they made a criticism. Perhaps if you could explain the context and
removed the typos it might help. Also the namespace helps too.
 
I

imutate

I changed it back to composition and now I get a different error
message at runtime. Doing a resize on a std::vector I get glibc
detected malloc memory corruption. The difference now is that I don't
use any arrays now, all std::vectors. Some of these are vectors of
structs.
 
D

Daniel T.

I changed it back to composition and now I get a different error
message at runtime. Doing a resize on a std::vector I get glibc
detected malloc memory corruption.

That's telling you that at some earlier time, you wrote to memory you
didn't have access to.
The difference now is that I don't
use any arrays now, all std::vectors. Some of these are vectors of
structs.

Now change all the op[] calls with at(). If you use all vectors, you can
do that with a global replace of '[' with "at(" and then globally
replace ']' with ")".

That will likely find your error real quick.
 
I

imutate

Now change all the op[] calls with at(). If you use all vectors, you can
do that with a global replace of '[' with "at(" and then globally
replace ']' with ")".

Do you mean change x to x(i) or something else ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,813
Messages
2,569,697
Members
45,488
Latest member
MohammedHa

Latest Threads

Top