0

I recently learned about the difference between the heap and the stack in c++, and how the stack is much more efficient and so should be used over the heap when possible. When I looked to see why one would want to ever use the heap, I saw this question, which said to use the heap when a variable is needed beyond a function's scope.

Example:

#include <iostream>

int* giveNumPtr()
{
    int* num = new int;
    *num = 10;
    return num;
}

int main()
{
    int* val = giveNumPtr();
    std::cout << *val << std::endl;
    *val = 3;
    std::cout << *val;
}

However, can't this be accomplished just as well on the stack?:

#include <iostream>

int* giveNumPtr()
{
    int num;
    num = 10;
    return &num;
}

int main()
{
    int* val = giveNumPtr();
    std::cout << *val << std::endl;
    *val = 3;
    std::cout << *val;
}

What am I missing, and what are the benefits of doing it the first way on the heap?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
linger1109
  • 500
  • 2
  • 11
  • You cannot accomplish with stack, but *static variable* may be useful according to the purpose. – MikeCAT Jul 21 '22 at 18:14
  • 3
    The 2nd way produces undefined behavior. You are returning the address of a local variable that goes away when the function returns. – jkb Jul 21 '22 at 18:15
  • If you turn on compiler warnings, it will probably tell you about the issue in your code. A different version would be to pass a reference as parameter to the function instead and store the result in there. – Ulrich Eckhardt Jul 21 '22 at 18:16
  • 2
    Until you are more familiar with Undefined Behavior, I recommend not worrying about "more efficient". Your code should be correct first and efficient second. – Drew Dormann Jul 21 '22 at 18:26
  • In general, the benefits of the *heap* are that it is allocated a lot more memory than the *stack*. The common purpose of the `stack` is to hold temporary variables and some addresses (like the return address). – Thomas Matthews Jul 21 '22 at 19:08

1 Answers1

1

In the first program you should delete the allocated memory

int main()
{
    int* val = giveNumPtr();
    std::cout << *val << std::endl;
    *val = 3;
    std::cout << *val;

    delete val;
}

In the second program the function returns a pointer to a local variable with automatic storage duration.

int* giveNumPtr()
{
    int num;
    num = 10;
    return &num;
}

It means that the variable will not be alive after exiting the function and as a result the returned pointer will be invalid.

You could write for example

int* giveNumPtr()
{
    static int num;
    num = 10;
    return &num;
}

But in this case you will be unable to reallocate memory.

As for your general question then consider building a list or other container that can hold a varying number of elements. How can you place all its elements in the stack and change their numbers?

You should investigate standard containers as for example std::vector or std::list or for example std::set.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335