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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:31:47+00:00 2026-06-04T18:31:47+00:00

In C++, how does a function treat memory that has been dynamically allocated upon

  • 0

In C++, how does a function treat memory that has been dynamically allocated upon exiting the scope of the function? Is this memory cleared, or can it be passed back into the main block?

In context: I have a function, and I pass it a pointer to double to serve as an array. I dynamically allocate this memory within the function, initialise the elements and the exit the function.

            void my_func(double* ptr){
            ptr = new double[2];
            ptr[0] = 15; ptr[1] = 10;
            }

In the main block, I then use the newly allocated array.

            int main(){
            double* ptr;
            my_func(ptr);
            cout << ptr[0] + ptr[1] << endl;
            delete[] ptr;
            return 0;

Will this work? Are there dangers/pitfalls associated with this approach?

  • 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-04T18:31:49+00:00Added an answer on June 4, 2026 at 6:31 pm

    In C++, how does a function treat memory that has been dynamically allocated upon exiting the scope of the function? Is this memory cleared, or can it be passed back into the main block?

    In C++, memory that has been manually (dynamically) allocated has to be manually deallocated.

    In context: I have a function, and I pass it a pointer to double to serve as an array. I dynamically allocate this memory within the function, initialise the elements and the exit the function.

    You are taking the pointer by value, so while you can change the contents of what the pointer points to you cannot change the pointer itself. Doing so changes only the local copy of the pointer. If you take the pointer by reference, then it would work:

    void my_func(double*& ptr)
    {
        ptr = new double[2];
        ptr[0] = 15; ptr[1] = 10;
    }
    

    Will this work? Are there dangers/pitfalls associated with this approach?

    It will mostly work, but its not the way to go in C++ because of the associated pitfalls. Better go with a vector:

    std::vector<int> my_func()
    {
        std::vector<int> buffer;
        buffer.push_back(15);
        buffer.push_back(10);
        return buffer;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone explain what the following PHP Code does function query($query_string) { if ($query_string
This code does not function as expected: // $field contains the name of a
* Simple question : Why does this function throw an exception when evaluated ?
How does define() treat traditional javascript files? For example, a.js define(['b', 'c.js', 'd.js'], function(b){
When externing a function in the cpp file does the compiler treat these differently?
This question has been bothering me for a while. If I do int* a
Does anyone have a PHP function that for example if a mysql_error() happens due
It seems that Common Lisp does treat (list 'quote 'x) in a special way.
In a C program, how does function call by value work, and how does
Does such function exist? I created my own but would like to use an

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.