Click here to Skip to main content
14,922,447 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

Just a quick memory management question, let's say I have the following:

const std::vector<int> intArray = {1, 2, 3};
delete intArray;
This should allocate memory for the array then free it up again.

How does this work in this situation?
doSomething({1, 2, 3});

void doSomething(const std::vector<int> arr)
{
        // whatever
}
Here, there's no opportunity to delete the original array. Which of the following should I do?

1. Nothing, that memory will be managed already.

2.
const std::vector<int> arr = {1, 2, 3};
doSomething(arr);
delete arr;

void doSomething(const std::vector<int> arr)
{
        // whatever
}

3.
const std::vector<int>* arr = {1, 2, 3};
doSomething(arr);

void doSomething(const std::vector<int>& arr)
{
        delete arr;
}


I'm not sure if the syntax for 1 and 2 are correct either, please notify me of any issues you see there.

What I have tried:

------------------------------------------------------------------------------
Posted
Updated 23-May-18 5:09am
v2

Have you tried to compile this code:

const std::vector<int> intArray = {1, 2, 3};
delete intArray;


I think no, because you should get an error on "delete intArray" like "Expression must be a pointer".

You can only delete the objects created on the heap with "new", not such stack objects like intArray: take a look at operator delete.
   
Comments
[no name] 23-May-18 11:06am
   
Ah ok, this is where things differ to other languages I'm used to, something like
Object obj;
in another language would create the object but not construct it, and you'd have to have
obj = new Object();
afterwards. From what I understand, simply something like
Object obj;
in C++ does all of that already.

So doing that creates the variable on the stack, whereas
Object obj = new Object();
would create the variable on the heap and would therefore require
delete obj;


When using 'new' with no arguments, do you have to have the () after the object type or can you leave it as without, like this?

Object obj = new Object;
Rick York 23-May-18 12:41pm
   
One thing to keep in mind is with C++, the operator new creates an object and returns a pointer to it. Your statement should be:
Object * pobj = new Object;
The parenthesis are needed only if you are passing parameters to the object's constructor like:
Object * pobj = new Object( param1, param2 );
If you declare an object on the stack as in your case, it is destroyed automatically when going out of scope.

As already noted at your other question, you should pass it by reference to avoid creating a copy.

So it should be:
C++
{
    const std::vector<int> arr = {1, 2, 3};
    doSomething(arr);

    // arr goes out of scope here at the end of the block
    // The desctructor is called and any memory is freed
}

void doSomething(const std::vector<int>& arr)
{
    // do something
}

If you pass the array by value:
C++
// A temporary copy of the passed array is created and assigned to arr
void doSomething(const std::vector<int> arr)
{
    // do something

    // The destructor of arr is called here when returning from the function
    // The original array passed from the caller still exists
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900