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

  • Home
  • SEARCH
  • 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 93923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:25:14+00:00 2026-05-10T23:25:14+00:00

While learning different languages, I’ve often seen objects allocated on the fly, most often

  • 0

While learning different languages, I’ve often seen objects allocated on the fly, most often in Java and C#, like this:

functionCall(new className(initializers)); 

I understand that this is perfectly legal in memory-managed languages, but can this technique be used in C++ without causing a memory leak?

  • 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. 2026-05-10T23:25:15+00:00Added an answer on May 10, 2026 at 11:25 pm

    Your code is valid (assuming functionCall() actually guarantees that the pointer gets deleted), but it’s fragile and will make alarm bells go off in the heads of most C++ programmers.

    There are multiple problems with your code:

    • First and foremost, who owns the pointer? Who is responsible for freeing it? The calling code can’t do it, because you don’t store the pointer. That means the called function must do it, but that’s not clear to someone looking at that function. Similarly, if I call the code from somewhere else, I certainly don’t expect the function to call delete on the pointer I passed to it!
    • If we make your example slightly more complex, it can leak memory, even if the called function calls delete. Say it looks like this: functionCall(new className(initializers), new className(initializers)); Imagine that the first one is allocated successfully, but the second one throws an exception (maybe it’s out of memory, or maybe the class constructor threw an exception). functionCall never gets called then, and can’t free the memory.

    The simple (but still messy) solution is to allocate memory first, and store the pointer, and then free it in the same scope as it was declared (so the calling function owns the memory):

    className* p = new className(initializers); functionCall(p); delete p; 

    But this is still a mess. What if functionCall throws an exception? Then p won’t be deleted. Unless we add a try/catch around the whole thing, but sheesh, that’s messy. What if the function gets a bit more complex, and may return after functionCall but before delete? Whoops, memory leak. Impossible to maintain. Bad code.

    So one of the nice solutions is to use a smart pointer:

    boost::shared_ptr<className> p = boost::shared_ptr<className>(new className(initializers)); functionCall(p); 

    Now ownership of the memory is dealt with. The shared_ptr owns the memory, and guarantees that it’ll get freed. We could use std::auto_ptr instead, of course, but shared_ptr implements the semantics you’d usually expect.

    Note that I still allocated the memory on a separate line, because the problem with making multiple allocations on the same line as you make the function call still exists. One of them may still throw, and then you’ve leaked memory.

    Smart pointers are generally the absolute minimum you need to handle memory management. But often, the nice solution is to write your own RAII class.

    className should be allocated on the stack, and in its constructor, make what allocations with new are necessary. And in its destructor, it should free that memory. This way, you’re guaranteed that no memory leaks will occur, and you can make the function call as simple as this:

    functionCall(className(initializers)); 

    The C++ standard library works like this. std::vector is one example. You’d never allocate a vector with new. You allocate it on the stack, and let it deal with its memory allocations internally.

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

Sidebar

Ask A Question

Stats

  • Questions 84k
  • Answers 84k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The problem is that you (I) are note passing all… May 11, 2026 at 4:57 pm
  • Editorial Team
    Editorial Team added an answer Microsoft Research put one together. Don't know how easy it… May 11, 2026 at 4:57 pm
  • Editorial Team
    Editorial Team added an answer This page on the CocoaDev wiki contains several implementations of… May 11, 2026 at 4:57 pm

Related Questions

While learning different languages, I've often seen objects allocated on the fly, most often
Is is that I'm a newbie learning Ruby, or does it really have more
<edit> Thanks to everyone who has answered so far. The zip and os.path.join are
I've been using javascript for a while, but have never learned the language past
I recently started learning Python and I was rather surprised to find a 1000

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.