Jump to content
  • Advertisement
Sign in to follow this  
Defeatist

C++ vector out of range problem/segfault

This topic is 3115 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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]

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!