0

Learning pointers for the first time. So ptr is being assigned n, n1 and finally n2 but n and n1 were never deleted. Hope that makes sense.

#include <iostream>
using namespace std;

int main() {
    
    int n = 5;
    int n1 = 7;
    int n2 = 8;
    int *ptr;
    ptr = &n;
    ptr = &n1;
    ptr = &n2;

    cout << ptr << endl;
    cout << *ptr << endl;

    return 0;
}
D Yuri
  • 19
  • 9
    No, there is no stack overflow nor memory leak here. You not only don't have to `delete n` or `n1`, you are not allowed to do that. – Yksisarvinen Jul 17 '22 at 19:11
  • If you're learning about pointers also have a look at std::unique_ptr and std::make_unique. In current C++ the use of new/delete is no longer recommended. – Pepijn Kramer Jul 17 '22 at 19:14
  • `n`, `n1`, and `n2` all have automatic storage duration. That means that they're destroyed automatically when their lifespan ends, which in this case means "when `main` returns". – Nathan Pierson Jul 17 '22 at 19:15
  • They were also not dynamicaly allocated/newed, so not being deleted is ok here. delete's need to match new's. n, n1, & n2 go away when the function they are a part of exits. What would be a problem is if this was in a function & a pointer ro one of them was kept around by the calling function. That is called a dangling pointer because what it pointed to is no longer guaranteed to be there. – Avi Berger Jul 17 '22 at 19:16

2 Answers2

1

The stack is generally a (relatively) small, fixed sized, area of memory allocated to each thread in your application. The stack memory used by a function is automatically released at the end of that function.

A stack overflow is when your program runs out of stack memory. This generally occurs for two reasons:

  1. A large object is created on the stack, e.g. an array of 1,000,000 ints might use up 4mb of stack memory but on Windows the default stack size is usually 1mb so your program would encounter a stack overflow when the array is created.
  2. Too many levels of function calls occur (e.g. infinite recursion). Each time you call a function an amount of stack memory is used to store the variables of that function along with parameters, return addresses etc. Depending on the amount of memory used by each function, the number of nested function calls you can do without causing a stack overflow will vary. E.g. if you create large arrays on the stack you'll be able to do far fewer levels of recursion than if each function just has a few integer variables.

Neither scenario is occurring in your code, you're creating 4 variables on the stack and assigning values to them. The behaviour is well defined and the memory will be automatically released at the end of main.

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
  • " The stack memory used by a function is automatically released at the end of that function." Stack space may be allocated at every new block scope and released afterwards. So a function is a bit to *special*, as a block can also be a subset inside a function... – Klaus Jul 21 '22 at 12:33
  • @Klaus none of this is specified by the standard, the variable will certainly be destructed at the end of the scope but I think some or maybe all compilers allocate a fixed amount of stack memory which variables are placed in, the used stack memory doesn't generally change during a function – Alan Birtles Jul 21 '22 at 12:38
0

OKAY. Firstly, you did not assign the pointer ptr to &n, &n1 and &n2. You were simply overriding the assignments so at the end of the code, ptr was only assigned to &n2.

Secondly, MEMORY OVERFLOW occurs when there is a memory leak and this happens when you use new keyword to allocate memory and do not use delete to deallocate it.

#include<iostream>
using namespace std;

int main()
{
    int* pointer;
    pointer=new int;
    *pointer=24;
    cout<<*pointer;
    delete pointer;

    return 0;
}

Omitting the delete pointer; in this case would be an example of a memory overflow.

However, stack overflow is a different thing and it does not apply here.

I hope this helps!

  • No need to have a memory leak to run out of memory, especially on the stack which is usually small. I'd have to disagree with `above code is the proper way of allocating and deallocating memory`, using raw new and delete is not the recommended practice in modern c++ especially where automatic stack allocations could be used instead – Alan Birtles Jul 21 '22 at 12:13
  • "in this case would be an example of a memory overflow." also not true, it is a leak! – Klaus Jul 21 '22 at 12:38