-1

For instance:

void Func()
{
    int* i = new int; // the simplest case

    vector<int*> v = new vector<int*>; // another case, not sure if **new** is using correctly here
    vector<int*>* vPointer = new vector<int*>; 
}

void main()
{
    Func();
}

So, if I allocate dynamic memory(by using new operator) for local variables in functions,

  1. Do they live on heap or stack?
  2. when the program exits the function, are they still dangling around on heap or destroyed as function variables?
  3. Can I use new operator on non-pointer types? How to delete (or return back the allocated heap memory) it later?

Thank you!

Eyez zeyE
  • 23
  • 5
  • should read: https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new – 463035818_is_not_a_number May 28 '20 at 15:21
  • Pointers in your example live on the stack. The integer and vectors will be allocated on a heap. – DNT May 28 '20 at 15:22
  • 1
    SO is not a replacement for a textbook. And there is no heap or stack in C++. – Slava May 28 '20 at 15:22
  • @Slava Well, there is `std::make_heap` and `std::stack`. – François Andrieux May 28 '20 at 15:35
  • `new` does not allocate memory for the variables, it allocates things in "the free store" whose locations (addresses) are the values of the variables. (Your home address written down on a piece of paper is not the house where you live, and destroying the paper does not make you homeless.) – molbdnilo May 28 '20 at 15:37
  • @idclev463035818 Thank you but it's not actually what I asked. My question is what exactly is the identity of a variable when it's init with new but inside of a function. If my english confused you I'm sorry. – Eyez zeyE May 29 '20 at 00:30
  • no it didnt confuse me. You should not use `new` whether inside a function or not and I thought that Q&A could help you. It isnt the same question but if you read the answers carefully you will also find your answer – 463035818_is_not_a_number May 29 '20 at 05:14

1 Answers1

2
int i = 3;

this creates an object of type int on the stack. (The C++ standard uses the term "automatic storage", and I'm usually a stickler for proper use of formal terms, but "stack" and "heap" are deeply embedded in programming vocabulary, so "stack" and "heap" are just fine as technical terms).

int *ip = 0;

this creates an object of type int* on the stack. ip holds a null pointer.

int *ip = new int;

this creates an object of type int* on the stack, just like the previous one. It holds a pointer to memory on the heap (see earlier parenthetical; the formal term is "free store"). You know that the memory is on the heap because that's what operator new does.

When you're finished with the heap memory you have to release it:

delete ip;

if you don't delete it and the function that created it returns, the local variable (ip) that held the pointer is gone. The memory has been allocated and it hasn't been freed, so unless you copied the pointer to some place else, you have no way to get at the memory you allocated. That's called a "memory leak".

Pete Becker
  • 69,019
  • 6
  • 64
  • 147