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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:43:19+00:00 2026-05-22T02:43:19+00:00

A memory leak exists in the following function. The trouble I am having is

  • 0

A memory leak exists in the following function. The trouble I am having is knowing how, when, where, and what to delete. Here is the code:

#include "stdafx.h"
#include <iostream>

void someFunc(double** ppDoubleArray, int length)
{
    double* pNewDoubleArray = new double[length];

    for(int i = 0; i < length; i++)
    {
        pNewDoubleArray[i] = 3 * i + 2;
    }

    *ppDoubleArray = pNewDoubleArray;
}
int main()
{
    double dbls[] = { 1, 2, 3, 4, 5 };
    double* pArray = dbls;

    int length = sizeof dbls / sizeof dbls[0];

    std::cout << "Before..." << std::endl;

    for(int i = 0; i < length; i++)
    {
        std::cout << pArray[i] << ", ";
    }

    std::cout << std::endl;

    someFunc(&pArray, length);

    std::cout << "After..." << std::endl;

    //Expected series is: 2, 5, 8, 11, 14

    for(int i = 0; i < length; i++)
    {
        std::cout << pArray[i] << ", ";
    }

    std::cout << std::endl;

    while(true){ }

    return 0;
}

As you can see, I tried what to delete the new array I allocated after I had used it. It actually makes sense that this didn’t work, but I am not sure what to do here..

Added delete[] pArray:

#include "stdafx.h"
#include <iostream>

void someFunc(double** ppDoubleArray, int length)
{
    double* pNewDoubleArray = new double[length];

    for(int i = 0; i < length; i++)
    {
        pNewDoubleArray[i] = 3 * i + 2;
    }

    *ppDoubleArray = pNewDoubleArray;
}
int main()
{
    double dbls[] = { 1, 2, 3, 4, 5 };
    double* pArray = dbls;

    int length = sizeof dbls / sizeof dbls[0];

    std::cout << "Before..." << std::endl;

    for(int i = 0; i < length; i++)
    {
        std::cout << pArray[i] << ", ";
    }

    std::cout << std::endl;

    someFunc(&pArray, length);

    std::cout << "After..." << std::endl;

    //Expected series is: 2, 5, 8, 11, 14

    for(int i = 0; i < length; i++)
    {
        std::cout << pArray[i] << ", ";
    }

    delete[] pArray;

    std::cout << std::endl;

    while(true){ }

    return 0;
}

Does this solve any, if at all memory leaks in this situation?

  • 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-05-22T02:43:20+00:00Added an answer on May 22, 2026 at 2:43 am

    You are allocating & deleting an array in the function. And you are also returning it.

    int main()
    {
    

    // This one is allocated on the stack, so it will be deleted when exiting main()

        double dbls[] = { 1, 2, 3, 4, 5 };
    
        double* pArray = dbls;
    
        //...
    

    // Your function allocates some memory now pointed at by pArray

        someFunc(&pArray, length);
    
        std::cout << "After..." << std::endl;
    
        //Expected series is: 2, 5, 8, 11, 14
    
        for(int i = 0; i < length; i++)
        {
            std::cout << pArray[i] << ", ";
        }
    
        std::cout << std::endl;
    
        while(true){ }
    

    // Your forgot to delete the memory allocated by your function!!! Memory leak!!!

        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code snippet illustrates a memory leak when opening XPS files. If you
Hy all, I believe that the following piece of code is generating memory leak?
What memory leak detectors have people had a good experience with? Here is a
Possible Duplicate: how to find memory leak in c++ code/project I would like to
Consider the following C++ program: #include <cstdlib> // for exit(3) #include <string> #include <iostream>
I get memory leak warning when I addsubView to Current ViewController....This is my code..
I'm having memory leak / deleting errors when using MySQL Connector C++ 1.05. The
According to Instruments analysis, I have a memory leak here - and - I'm
I am experiencing a memory leak with a code similar to the one below
Memory leak problem - NSConcreteData // to set tip - photo in photo frame

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.