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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:24:19+00:00 2026-06-05T15:24:19+00:00

I want to pass an uninitialized object pointer to some method. Within that method

  • 0

I want to pass an uninitialized object pointer to some method. Within that method I’ll create an instance of the object using the new operator (or malloc) and assign its address to the passed pointer. This is part of my code:

void test(testClass* t){
    ...
    t = new testClass();
    ...
}

int _tmain(int argc, _TCHAR* argv[])
{
    testClass* t = NULL;
    test(t);
    cout<<t->getTestValue()<<endl;
    delete t;
}

My problem is in the _tmain function (after calling test) where I want to call the getTestValue method of the object pointed to by t. Here my program crashes and terminates with an access violation unexpected exception.

It seems an object created dynamically (using operator new and even malloc) is not usable outsie of the scope of the function test. Can anybody help?

  • 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-05T15:24:20+00:00Added an answer on June 5, 2026 at 3:24 pm

    To change a pointer in a function, it must be passed by reference:

    void test(testClass*& t)
    

    Otherwise, the change made inside the function is not visible on the outside.

    Your original pointer doesn’t get changed, remains NULL, and thus runs into undefined behavior and a crash.

    Whenever you pass a parameter by value, a copy is made inside a function. So when you do t = new testClass();, you’re actually working on a copy of your pointer.

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

Sidebar

Related Questions

want to pass boost::bind to a method expecting a plain function pointer (same signature).
I want pass a class-instace to a method of an other and be sure
I want to pass data from client side to server-side. I am using jQuery
I want to pass an IEnumerable collection from view to controller using JQuery from
I want to pass a custom object from one Activity to another, here is
I want to pass an argument to a function called using setTimeout . I
I want to pass some dynamic paramters to jquery onClick event. Before, I used
I want to pass the parameter that List.sortBy takes through to another function. My
I am creating a jasper report.In that I want to write one method which
I want to pass certain hidden parameters in a request when some conditions are

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.