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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:58:57+00:00 2026-06-10T13:58:57+00:00

I ran into this bizarre pointer-less situation in my code and am now wondering

  • 0

I ran into this bizarre pointer-less situation in my code and am now wondering if there is a way to detect it (other than crashing). The code manages to get into an un-constructed object’s method. When b1 is constructed, a has not been constructed, and b tries to use it. By the time b2 is constructed, a is properly constructed and the code works as expected.

Beyond the obvious, “don’t do this in your code,” I’m wondering if there’s a way to detect this at compile or run time. The compiler didn’t detect it at all, and I just got some obscure and very unhelpful message about running managed code while initializing a DLL when the program crashed.

I tried to test “this”, but it’s not NULL because memory has been asigned — it’s just that the constructor has not been called so the memory is in an indeterminate state.

I would have thought the compiler would stick in some debug code to detect when this happens, but I guess not.

Is there any assertion or test or compile time switch I can use to detect this situation, or does it just come down to, “if it hurts, don’t do that?”

 OUTPUT:
 (NULL)
 test

#include "stdafx.h"
#include "cstring"

class Apple 
{
    char *sometimesinitialized;

    public:
    Apple () { 
        sometimesinitialized = new char[15];
        strcpy_s(sometimesinitialized, 5, "test");
    };
    void test()
    {
        printf("%s\n", sometimesinitialized);
    }
};

class Ball
{
    public:
    Ball();
};

Ball b1; // OOPS!
Apple a;
Ball b2; // Works as expected

Ball::Ball()
{
    a.test();
}

int _tmain(int argc, _TCHAR* argv[])
{
    scanf_s("%i");
    return 0;
}
  • 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-10T13:58:59+00:00Added an answer on June 10, 2026 at 1:58 pm

    If an instance of B needs to access an instance of A then it should be passed to B‘s constructor.

    struct B {
        B(A &a) {
            a.test();
        }
    };
    
    
    A a;
    B b(a);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into this issue twice now, and am curious to know if there
I'm doing some testing and ran into this bizarre situation: The first case (assigning
I ran into this error when building my code with CLANG: In file included
I ran into this situation today. I have an object which I'm testing for
I ran into this RegExp /[[0]]/ in JavaScript, and have been wondering what it
I ran into this statement in a piece of code: Int32 medianIndex = colorList.Count
I ran into this situation that is weird, in order to kill the zombie
today I ran into this problem which really bugs me, as almost the code
I ran into this problem at my old job, and now again at my
I've ran into this scenario a few times now and I cant explain it.

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.