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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:36:27+00:00 2026-06-14T05:36:27+00:00

My program crashes because it reaches a stack.top() it shouldn’t reach, as the stack

  • 0

My program crashes because it reaches a stack.top() it shouldn’t reach, as the stack is empty.
I have an if which checks just that:

    if(!st.empty());
        //do stuff

(I have initialized

stack<int> st;

).

But although I can see in the debug that the stack is empty, it still goes in the if!
I even wrote this code:

    if(st.size()>0);
        cout<<st.size();

And it prints 0!
What is going on and how can I fix it? Thanks!

  • 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-14T05:36:28+00:00Added an answer on June 14, 2026 at 5:36 am

    The semicolons after the if statements are the problem

    BAD:

    if(st.size()>0); // <-- this should not be here!!!!!!!!
        cout<<st.size();
    

    Properly rewritten:

    if(st.size()>0) {
        cout<<st.size();
    }
    

    Also, as @WhozCraig pointed out, the other statement has a semicolon too!

    BAD:

    if(!st.empty()); // <--BAD!
        //do stuff
    

    Good:

    if(!st.empty()) {
        //do stuff
    }
    

    ALWAYS!! use brackets with branches (if, switch), and loops (for, while, do-while)!!! It pays off big time! (Not to mention, a cute kitten dies each and every time such a block is written without brackets!) ALWAYS!!

    For example this can kill a day in debugging:

    BAD:

    int i=0;
    ...
    while(i++<1000);
        doStuff(i);
    

    Good:

    int i=0;
    ...
    while(i++<1000) {
        doStuff(i);
    }
    

    Beware (as @WhozCraig pointed out again) this does not automagically solve the problem of semicolon terminated branch and loop statements, as this is perfectly valid syntax:

    if (condition);{ ...code... } 
    

    Or

    if (condition);
    { 
         ...code... 
    } 
    

    But in my opinion and experience (this is totally subjective!) – as I myself have fallen into this trap a couple of times – I experienced that when I have the curly bracket after the aforementioned statements, I didn’t ever make the mistake of typing a semicolon again. Adhering to this convention was a silver bullet – for me, and others could benefit from this too. Also, if there was a semicolon there, it would immediately catch my eye, just by looking, as it is an “uncommon pattern of characters”.

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

Sidebar

Related Questions

My program crashes due to a null pointer exception. Snip of Stack Trace: java.lang.NullPointerException
I have a C++ program that crashed due to a bug. It would not
My multithreaded Java program crashes because it runs out of heap space and I
So, in my program i have a TabActivity which contains two tabs, one tab
The scenario: We have a python script that checks thousands of proxys simultaneously. The
I have a program that is constantly running. Normally, it seems to garbage collect,
We have a NET app that gets installed to the Program Files folder. The
I have an application which updates my datagrid each time a log file that
All my program crashes because of delete [] meanings; , delete [] meanings; ,
i have some problems in exporting crystal reports to pdf. My program crashes at

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.