Sign in to follow this  
Defeatist

C++ vector out of range problem/segfault

Recommended Posts

I have a problem with a vector going out of range. What i'm trying to do is loop through a vector that resembles my heightmap and assign a value I get from my perlin-noise map.
void mHeightMap::fillTerrain( LAND_TYPES type )
{
    float landMass = 0.0f;
    float singleElement = float( 1.0f / ( x_ * y_ ));

    if( type == BAY )
    {
        srand( (int)time(NULL) );
        Perlin noiseMap( 6,.39f,1,rand()%16);

        for(int i = 0; i < x_; i++)
        {
            for(int j = 0; j < y_; j++)
            {
                try
                {
                    float height = noiseMap.Get( float( i/x_ ), float( j/y_ ));

                    map.at(i + (j * x_)) = height;     

                    if( height >= 0.0f )
                    {
                        landMass += singleElement;
                    }
                }
                catch ( char* str )
                {
                    printf( str );
                }


            }
        }

        if( ( landMass < 0.25f ) || ( landMass > 0.75f ) )
        {
            map.clear();
            fillTerrain( type );
        }
    }
}
definition and constructor
mHeightMap::mHeightMap()
{
    x_ = 256;
    y_ = 256;
    z_ = 1.0f;
    map.resize( x_ * y_ * sizeof(float) );
}
class mHeightMap
{
public:
    mHeightMap();
    mHeightMap( int x, int y, float z );
    ~mHeightMap();

    int x_;
    int y_;
    float z_;


    void operator+= (mHeightMap);
    void operator*= (mHeightMap);
    void fillPerlin();
    void fillUniform();
    void fillVoronoi();
    void fillTerrain( LAND_TYPES type );

    private:
    vector<mTriangle> mesh;
    vector<float> map;


};
terminate called after throwing an instance of 'std::out of range' what(): vector::_M_range_check using GNU C++ Compiler

Share this post


Link to post
Share on other sites
Do you set the size of mHeightMap::map? Because when calling

map.at(i + (j * x_)) = height;



map must have at least
i + (j * x_) + 1
entries. This is probably not the case.

NB: Are you sure you want to use 'm' as a prefix for class names? Might be confusing for some as that is sometimes used to mark a variable as a member variable of a class.

(NB2: Seemed as if the forum somehow didn't like my plusses that's why i wrote them out.)
*edit: Thanks TheUnbeliver!

Cheers,
fng



[Edited by - fng on March 9, 2010 9:38:39 AM]

Share this post


Link to post
Share on other sites
Do you want to be using map's operator[]? It will insert the entry if it's not already present. (Disclaimer: I haven't read the code above, just an offhand suggestion based on the previous post.)

Quote:
Original post by fng
(NB2: Seemed as if the forum somehow didn't like my plusses that's why i wrote them out.)


It's a known* bug - they don't appear in preview, but otherwise will show up fine.

* for some definition of 'known', obviously!

Share this post


Link to post
Share on other sites
Quote:
Original post by TheUnbeliever
Do you want to be using map's operator[]? It will insert the entry if it's not already present. (Disclaimer: I haven't read the code above, just an offhand suggestion based on the previous post.)


'map' here is actually a std::vector, not a std::map instance. :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this