Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7056731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:54:07+00:00 2026-05-28T03:54:07+00:00

Valgrind detects an invalid read error I don’t know how to fix or to

  • 0

Valgrind detects an invalid read error I don’t know how to fix or to be more precise: I don’t know what the problem is.

Invalid read of size 8
 at 0x443212: std::vector<Tile*, std::allocator<Tile*> >::end() const
 by 0x44296C: Collection<Tile*>::Iterator::operator++()

The Iterator class is very simple (and actually a somewhat bad piece of programming) but sufficient for my needs right now. I think there are three methods you should know to hopefully help find my problem:

Iterator(size_t x, size_t y, const TileCollection& tiles)
        : mTiles(&tiles)
        , mX(mTiles->begin())
        , mY(mTiles->at(x).begin())
{   
       std::advance(mX, x); 
       std::advance(mY, y); 

       bool foundFirst = false;

       while (!foundFirst)
       {
              while (mY != mX->end() && *mY == 0) ++mY;

              if (mY != mX->end()) foundFirst = true;
              else
              {
                     ++mX;

                     if (mX != mTiles->end()) mY = mX->begin();
              }
       }
}

Iterator Iterator::operator++()
{
       bool foundNext = false;

       ++mY;

       while (!foundNext)
       {
              while (mY != mX->end() && *mY == 0) ++mY;

              if (mY != mX->end()) foundNext = true;
              else
              {
                     ++mX;

                     if (mX != mTiles->end()) mY = mX->begin();
              }
       }

       return *this;
}

void TileCollection::add(Tile* tile)
{
       Point2D p(tile->getPosition());

       std::vector<Tile*> tmp(1, (Tile*)0);

       if ((size_t)p.x >= mTiles.size())
              mTiles.resize(p.x + 1, tmp);
       if ((size_t)p.y >= mTiles.at(p.x).size())
              mTiles.at(p.x).resize(p.y + 1, (Tile*)0);

       mTiles.at(p.x).at(p.y) = tile;

       ++mNumTiles;
}

The actual code that is causing the valgrind error is the line:

while (mY != mX->end() && *mY == 0) ++mY;

…of the Iterator::operator++ method.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:54:07+00:00Added an answer on May 28, 2026 at 3:54 am

    It looks to me that, at the least, the following line in operator++

    if (mX != mTiles->end()) mY = mX->begin();
    

    is lacking a suitable else-clause.

    Consider what happens when mX actually reaches mTiles->end(): You will enter a new iteration of the outer while loop; the first line in that loop (the line that causes the Valgrind error) will evaluate mX->end() and thus attempt to dereference mX — but mX is mTiles->end(), and it’s not correct to dereference the end iterator of a collection since it doesn’t actually reference an element of the collection. It looks to me as if this may be the cause of your Valgrind error.

    (Note that the constructor contains essentially the same code.)

    More generally, I think you need to think about how you handle reaching the end of your two-dimensional array. How does the client of your Iterator check whether it has reached the end of the iteration? How do you expect your operator++ to handle the case when it reaches the end of the two-dimensional array? Should it protect itself against getting called too often?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason valgrind keeps throwing the following error: ==6263== Invalid read of size
Valgrind reports error Invalid read of size 8 in the following code. I have
valgrind report the invalid write of size 8 at 0x4007A0:ArrayCstringPush by 0x4008F8:main Address 0x4A0A450
Valgrind detect an invalid write of size 1 in this piece of code. In
Valgrind is showing an uninitialised value of size 8 error. And occasionally, the below
I'm running Valgrind and I'm getting the following error (this is not the only
I'm running valgrind and I'm getting the following error.. before I made a backup
I am aware of Valgrind, but it just detects memory management issues. What I
When running Valgrind's memcheck tool, I often get many hundreds of thousands (or more,
Valgrind throws me out this error: ==11204== Syscall param write(buf) points to uninitialised byte(s)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.