Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Segmentation fault when using a vector template

4 views
Skip to first unread message

Greg

unread,
Dec 24, 2006, 3:04:09 PM12/24/06
to
#include <vector>
#include <string>
#include <iostream>

template < typename T >
class BaseVec : public std::vector<T> {
public:
void setsize(const int &sz)
{ std::vector<T>reserve(sz); std::vector<T>resize(sz); }
BaseVec(): std::vector<T>() { }
};

class ttsmod
{
public :
std::string nm;
int cnt1, cnt2;
};

typedef int modidx;

class ttsmods : public BaseVec<ttsmod>
{
public :
ttsmods();
};

#define MAX_MODS 128

ttsmods::ttsmods()
{
setsize(MAX_MODS);
}

int main(int argc, char *argv[])
{

ttsmods mod;
std::cout << "test0" << std::endl;
mod[0].cnt1 = 0;
mod[0].cnt2 = 1;
mod[0].nm = "test1";
std::cout << "test1" << std::endl;

mod[1].cnt1 = 0;
mod[1].cnt2 = 1;
mod[1].nm = "test1";
std::cout << "test2" << std::endl;
}

test0 is displayed and then the Segmentation fault occurs.

Gianni Mariani

unread,
Dec 24, 2006, 3:37:19 PM12/24/06
to
Greg wrote:
> #include <vector>
> #include <string>
> #include <iostream>
>
> template < typename T >
> class BaseVec : public std::vector<T> {
> public:
> void setsize(const int &sz)
> { std::vector<T>reserve(sz); std::vector<T>resize(sz); }

a) You don't need to call reserve, resize does all that for you
b) You're missing "::" operators so what you're doing is defining two
objects locally, one called "reserve" and another "resize" and
initializing them to sz size. You're not calling resize at all.
c) Rather than scoping, you can use: this->resize(sz)
d) Try to do initial size manipulation in the constructor i.e.:

BaseVec( int sz ) : std::vector<T>( sz ) {}

> BaseVec(): std::vector<T>() { }

e) There is no need to specify the default initializer on std::vector

Greg

unread,
Dec 24, 2006, 4:15:12 PM12/24/06
to
It might clarify things if you rewrote the new BaseVec declaration with
these changes.

> a) You don't need to call reserve, resize does all that for you

OK

> b) You're missing "::" operators so what you're doing is defining two
> objects locally, one called "reserve" and another "resize" and
> initializing them to sz size. You're not calling resize at all.

A pitfall to be avoided.

> c) Rather than scoping, you can use: this->resize(sz)

OK, but where is this put.

> d) Try to do initial size manipulation in the constructor i.e.:

Does "std::vector<T>( sz )" replace resize ?

> e) There is no need to specify the default initializer on std::vector

Oh, I was misled on that one then.

OK, so if I base a class on this new BaseVec template, where do I
specify the actual size parameter.

Gianni Mariani

unread,
Dec 24, 2006, 4:26:32 PM12/24/06
to
Greg wrote:
...

>> c) Rather than scoping, you can use: this->resize(sz)
>
> OK, but where is this put.

You can place this where you had "std::vector<T>::resize(sz)".

>
>> d) Try to do initial size manipulation in the constructor i.e.:
>
> Does "std::vector<T>( sz )" replace resize ?

Yes - look at the definition of vector. The constructor with a size
sets the initial size of the vector.

>
>> e) There is no need to specify the default initializer on std::vector
>
> Oh, I was misled on that one then.


>
> OK, so if I base a class on this new BaseVec template, where do I
> specify the actual size parameter.

Just like vector, you should be allowed to change the size (using
resize) or initialize it. However, since you're inheriting std::vector,
all those methods are already available.


Greg

unread,
Dec 24, 2006, 4:55:14 PM12/24/06
to
Thanks, I think I have got this now and it can be done without a
template.
Do you know how to extend this to a matrix construction ?

public :
myclass(int sz1, int sz2) : std::vector<std::vector <sometype

..and then what ?

Can this be done because it seems to me it would need to iterate over
the rows.

Gianni Mariani

unread,
Dec 24, 2006, 5:19:53 PM12/24/06
to

There are a number of posts discussing the matrix implementation. The
FAQ has one (which I don't particularly agree with) and I have posted a
number of simple matrix template classes as have others.

There are plenty of "matrix" implementations you can draw from. Do look
at others.

This is what I posted in :
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a?hl=en&

#include <vector>

template <typename w_elem_type>
class matrix
{
public:
typedef int t_Size;

t_Size m_columns;
t_Size m_rows;

std::vector<w_elem_type> m_data;

matrix( t_Size i_columns = 0, t_Size i_rows = 0 )
: m_columns( i_columns ),
m_rows( i_rows ),
m_data( i_columns * i_rows )
{
}

w_elem_type * operator[]( t_Size i_index )
{
return & ( m_data[ i_index * m_rows ] );
}

template <typename w_Type, int w_columns, int w_rows>
matrix( const w_Type (&i_array)[w_columns][w_rows] )
: m_columns( w_columns ),
m_rows( w_rows ),
m_data( & (i_array[0][0]), & (i_array[w_columns-1][w_rows]) )
{
}

};

#include <iostream>

double array[3][4] = {
{ 1.0, 2.0, 3.3, 4.4 },
{ 1.0, 2.0, 3.3, 4.4 },
{ 1.0, 2.0, 3.3, 4.5 },

};

int main()
{
matrix<float> mat1( 3, 4 );
matrix<float> mat2;
matrix<float> mat3( array );

mat2 = mat3;

std::cout << mat2[2][3] << "\n";

}

0 new messages