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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:37:45+00:00 2026-06-11T07:37:45+00:00

int main(){ MyBase *mb; int choice; cout << Select: ; cin >> choice; switch

  • 0
int main(){
MyBase *mb;
int choice;

cout << "Select: ";
cin >> choice;

switch (choice) {
    case 1:
        mb = new Test1();
        break;
    case 2:
        mb = new Test2();
        break;
    case 3:
        mb = new Test3();
        break;
    case 4:
        mb = new Test4();
        break;
    case 5:
        mb = new Test5();
        break;
    case 6:{
        LinkTest t;
        t.start();
        break;
    }
    default:
        return 0;
}

if(mb != 0){
    mb->start();
    delete mb;     
}

return 0;
}

If choice is 6, why am I getting a segmentation fault when checking if mb is NULL?

I am new to c++ and just doing some exercises.
How can I fix this without affecting the class LinkTest?
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-11T07:37:46+00:00Added an answer on June 11, 2026 at 7:37 am

    When you declare variables inside function scope, they are normally whats called “stack variables”. This means they reside in stack-memory which typically contains lots of “garbage” from previous use. Hence, when not initializing the ‘mb’ variable upon declaration, the initial value is undefined (whatever happened to be lying around on that spot in the stack), typically non-zero.
    Therefore you should initialize mb upon declaration:

    MyBase* mb = 0;
    

    As an aside, I noticed that the ‘case 6:’ inside the ‘switch’ doesn’t set mb to anything…

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

Sidebar

Related Questions

int main() { cout<<Enter n; cin >> n; int * a = new int[n];
int main() { string line; char buff[10]; for(int i=0; i<10;i++) { cin.get(buff[i]); cout.put(buff[i]); if(i==10)
int main() { char *arr=\\0\1\8234\0; int i=0; while(arr[i]) { switch(arr[i]) { case '0': printf(is
int main() { clrscr(); char c[80],d[80]; cout<<Enter a string = ; cin.get(a,80); strcpy(c,a); strrev(a);
int main() { int b[2][3]={{1,2},{3,4,5}}; cout << b[0][2] << endl; } And the result
int main() { Employee *e = new Employee(); delete e; delete e; ... delete
int main(){ char ch; fork(); cin >> c; } After calling fork() I should
int main() { int main=5; printf(%d,main); return 0; } In this case there is
int main (int argc, char* argv[]) { QApplication app(argc, argv); QTextStream cout(stdout, QIODevice::WriteOnly); //
int main() { int i=3; (i << 1); cout << i; //Prints 3 }

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.