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

  • SEARCH
  • Home
  • 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 9292441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:52:49+00:00 2026-06-18T20:52:49+00:00

I have a piece of code that is creating a tile based level. class

  • 0

I have a piece of code that is creating a tile based level.

class Level {

//Variables
//===================================================
public:
    Tile *** TileGrid;  //A 2d array of pointers to tiles
    int TilesWide, TilesTall;

//Methods
//===================================================
public:
    Level::Level(char * fileName);
    Level::~Level();
    void Draw();
};

I allocate memory for the TileGrid and all is good. I have a destructor set up for the class too.

Level::~Level() {

    for (int i = 0; i < TilesTall; i++) {
        for (int j = 0; j < TilesWide; j++)
            //delete the looped tile being pointed to
            delete TileGrid[i][j];

        //delete the row
        delete [] TileGrid[i];
    }

    //delete the array of rows
    delete [] TileGrid;
}

For giggles I decided I would delete my instance of Level. After I did so, I found that I could still call its Draw method.

In the debugger the values for TilesWide and TilesTall are a huge negative numbers, so nothing draws in my for loops iterating the grid.

Would trying to access a deleted variable not cause some sort of crash?

  • 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-06-18T20:52:50+00:00Added an answer on June 18, 2026 at 8:52 pm

    What you have done is known as “Undefined behavior”.

    And undefined behavior doesn’t mean “will crash”, it means anything can happen. The program could format your hard drive, it could play a Bach sonata, it could draw a picture of daffy duck on the screen, it could give you a negative value, and in every case it would be exactly what you asked for.

    Not crashing is included, as is crashing.

    Now let us suppose a reasonably sane implementation of C++. The method Level::Draw does not vary with which instance of Level you pass it, so what function you call doesn’t rely on the instance. The instance is passed as a parameter to this function as a pointer that points to some memory in the heap that used to be a copy of the variable. Since then, it has been recycled for some other purpose, or maybe contains book-keeping information about the heap — what is there is undefined. (And, accessing it could segfault if the runtime returned the page to the system)

    You then proceed to interpret that garbage as some values. Things are perfectly fine, because random garbage looks like a C++ integer (in this case, as a negative integer) pretty much always on most systems.

    Now, once you start dereferencing pointers (and as such, accessing “random” parts of memory), or writing to the state of this, crashing is likely to happen (if not then, then later, or elsewhere).

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

Sidebar

Related Questions

I have this piece of code that does not work: public CartaoCidadao() { InitializeComponent();
I have a piece of code that was written by someone else before the
I have this piece of code that works fine in subsonic 2.2, I migrated
I have a piece of code that load a very big image in memory.
I have a piece of code that takes several rows from a database and
I have a piece of code that look similar to this: <xsl:choose> <xsl:when test=some_test>
I have a piece of code that calls a WCF service that is hosted
If have a piece of code that gets some data from a sql database
I have a piece of code that converts an infix expression to an expression
I have this piece of code that is giving me trouble. I know all

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.