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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:01:47+00:00 2026-06-10T22:01:47+00:00

Here, i am trying to create a Nth level hierarchy but doesnt let me

  • 0

Here, i am trying to create a Nth level hierarchy but doesnt let me point to outer-class of an inner class and getting an access violation error. But the latter version workes.

What is my mistake? Is this about the scope of newly created inner-loops? But they are created inside the class so it shouldnt be problem should it?

 // atom.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<stdlib.h>

class a
{
public:
    int x;
    a * inner;
    a * outer;
    a(int n)   //creates an inner a
    {
        n--;
        x=n;    
        if(n>0){inner=new a(n);}else{inner=NULL;}   
        inner->outer=this;//Unhandled exception at 0x004115ce in atom.exe: 0xC0000005:
                          //Access violation writing location 0x00000008.
    }

};

int main()
{
    a * c=new a(5);
    a * d=c;
    while((d->inner))     //would print 4321 if worked
    {
        std::cout<<d->x;
        d=d->inner;
    }
    getchar();
    delete c;
    d=NULL;
    c=NULL;
    return 0;
}

But this works:

// atom.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<stdlib.h>

class a
{
public:
    int x;
    a * inner;
    a * outer;
    a(int n)   //creates an inner a
    {
        n--;
        x=n;    
        if(n>0){inner=new a(n);inner->outer=this;}else{inner=NULL;} 
        //works without error
    }

};

int main()
{
    a * c=new a(5);
    a * d=c;
    while((d->inner))     //prints 4321
    {
        std::cout<<d->x;
        d=d->inner;
    }
    getchar();
    delete c;
    d=NULL;
    c=NULL;
    return 0;
}

Do you think all they are auto-deletet when i just delete c ?

  • 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-10T22:01:48+00:00Added an answer on June 10, 2026 at 10:01 pm

    When you do this:

    if(n>0)
    {
       inner=new a(n); //first n is 4, then 3,2,1 and then 0
    }
    else
    {
       inner=NULL;
    }   
    inner->outer=this;
    

    the condition n>0 will eventually not hold (at the 5th call), so inner will be NULL, and then you runt into undefined behavior (and the crash) when you attempt to dereference it (inner->outer).

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

Sidebar

Related Questions

Here's the response I keep getting when trying to create a new activity: {error:{errors:[{message:Unknown
I am trying to create an class object to a class but I keep
I am trying to create a table that I found here but I have
I'm trying to create project using template from here but when i'm trying to
Ok so I am trying create a login script, here I am using PHP5
I am trying to create auto-vectorized code with clang-3.2 From the slides here -
I'm trying to create a web application project template for everyone to use here
i am trying to create a query on my simpleDB. here is the query:
I am trying to create a simple drawing game like DrawSomething. The problem here
I'm trying to create a layout similar to this: alt text http://img20.imageshack.us/img20/3533/stackn.png Here's the

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.