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 9199427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:32:06+00:00 2026-06-17T22:32:06+00:00

Shouldn’t I get an error at line 8 of main.cpp? I am not using

  • 0

Shouldn’t I get an error at line 8 of “main.cpp”?

  • I am not using a destructor.
  • I think it has something to do with the “const” keyword.

main.cpp:

#include "stack.hpp"
int main() {
    node* firstnode = new node(NULL, 5);
    std::cout << firstnode -> getvariable() << std::endl;
    node* secondnode = new node(NULL, 10);
    std::cout << secondnode -> getvariable() << std::endl;
    delete firstnode;
    std::cout << firstnode -> getvariable() << std::endl;
    return 0;
}

stack.hpp:

#ifndef stack_hpp
#define stack_hpp
#include <iostream>
class node {
public:
    node(node* nextnode, int variablevalue);
    void setvariable(const int variablevalue);
    const int getvariable() const;
private:
    node* nextnodelink;
    int variable;
};
#endif

stack.cpp:

#include "stack.hpp"
node::node(node* nextnode, int variablevalue)
: nextnodelink(nextnode), variable(variablevalue) {
}

const int node::getvariable() const {
    return variable;
}
  • 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-17T22:32:07+00:00Added an answer on June 17, 2026 at 10:32 pm

    When you delete an object, the memory that object occupies will be put back to be “available”. So that computers run fast, the design of the “make it available” is such that it doesn’t do very much other than add the freed object to a linked list or something like that. It doesn’t, for example, fill it with nonsense so that it becomes unusable. The point with C++ is that it’s fast – to make every delete fill the object will nonsense just to make sure you can’t use it later would slow it down.

    Of course, it may be that, if you make some other allocations, the contents of the object is indeed “bad”. This is why it’s “undefined”. The standard doesn’t say what will happen, and almost anything CAN happen. Computer catching fire is unlikely, but the C standard doesn’t in itself have any wording to say “it must not cause computer to catch fire” or anything of that sort.

    A good rule of thumb is that if you want to make your code safe, set the pointer to NULL after you deleted it. That way, your prgram will most likely crash when the pointer is being used.

    For example:

    delete firstnode;
    firstnode = NULL;
    std::cout << firstnode -> getvariable() << std::endl;
    

    Now, your program should crash when you call “getvariable”. It’s not 100% sure to always do that. If you have a function that returns a constant for example, it may still “work”.

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

Sidebar

Related Questions

I know destructor shouldn't not throw exception. http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.13 I have the following code :
Shouldn't I get an error if my string goes over 9 characters long in
This shouldn't get the song name when iTunes is closed because iTunes has to
I know you shouldn't use this to decide whether or not to change an
Is this something I shouldn't try?
Shouldn't this be on the LogCat under info? It's not coming up protected void
I know I shouldn't be outputting things directly in PHP, but using a template
I know that I shouldn't throw exceptions from a destructor. If my destructor calls
Shouldn't it return a DRAW? def alphabeta(alpha, beta, player) best_score = -INFINITY if not
Shouldn't the Log method block? namespace Sandbox { class Program { static void Main(string[]

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.