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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:53:19+00:00 2026-06-03T09:53:19+00:00

My question is what happens to the object allocated with the new operator that

  • 0

My question is what happens to the object allocated with the new operator that is inside a function call.

A specific example: I have a private vector pV which I want to send to a object/function outside of the class, foo->func(std::vector<int> *vec). I first tried to write

foo->func( new std::vector<int>(pV) )

but this resulted in a memory leak (when said function is called repeatedly inside a loop). When I specifically created a new object, called the function and then deleted that object, the whole thing worked, without the memory leak.

Shouldn’t the newly created object ‘expire’ and be deleted when the function returns? If not, how should I delete the object, from inside the called function? And which is the better 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-03T09:53:20+00:00Added an answer on June 3, 2026 at 9:53 am

    Objects allocated with new must eventually be freed with delete, or there will be leaks. Allocation with new is independent of function calls – you can create something with new in one function and free it with delete in another without problems.

    If you want an object that is allocated in a function and freed when the function exits, just do this:

    void foo(...) {
        // some code
    
        MyClass myobj(...); // object allocated here
        // some more code
        return; // object freed here (or whenever function exits)
    }
    

    If you need to pass a pointer to your object to a function, you needn’t use new for that either; you can use the & operator:

    std::vector<int> myvec(pV);
    foo->func(&myvec);
    

    In this case myobj is an automatic variable which is placed on the stack and automatically deleted when the function exits. There is no need to use new in this case.

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

Sidebar

Related Questions

I am working with a std::vector to hold some objects that have dynamically allocated
The question in broad terms: I have an object with a method that performs
Java 6 API question. Does calling LockSupport.unpark(thread) have a happens-before relationship to the return
I just have a question in regards to threads that run concurrently and the
When operator new() is used with reference type, space for the instance is allocated
The question is simple: what is lifetime of that functor object that is automatically
What happens when an object which has a function currently being executed has all
I have a question regarding Object type casting. Suppose we have: A a =
I have a question. Imagine you'd have an object you'd want to save in
I have a two part question. >>> class One(object): ... pass ... >>> class

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.