C++ vector out of range problem/segfault

Started by
2 comments, last by Zahlman 14 years, 1 month ago
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
Advertisement
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]
-- blog: www.fysx.org
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!
[TheUnbeliever]
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. :)

This topic is closed to new replies.

Advertisement