How to fix 'address of stack memory associated with local variable' when returning a variable address from function?

时间:2019-04-16 23:41:20

标签: c++ pointers

My prof. had me copy and paste the code below she wrote and it kept on giving me bugs.

By running her program, the output was 1 for any value.

Logically, I understand how the function should return the address and in the main it prints the value of it. Theoretically, that is.

So here's what I tried so far:

1) Simply removing the statement p = cube(&n) and replacing with:

    std::cout << "Cube of " << n << " is " << cube(&n) << std::endl;

This worked.

2) In order to solve the "local variable" error, I made 'result' a global variable.

3) In cube(), i did:

int *cube(int *number)
{
    int result = (*number) * (*number) * (*number);
    int *newResult = &newResult;
    return newResult;
}

...but it outputted 1 for any integer.

This is the code sample she shared.

#include <iostream>

int *cube(int *);

int main()
{
    int n, *p;
    std::cout << "Enter an int: ";
    std::cin >> n;

    p = cube(&n);

    std::cout << "Cube of " << n << " is " << *p << std::endl;

    return 0;
}

int *cube(int *number)
{
    int result = (*number) * (*number) * (*number);
    return &result;
}

According to her, the output of this program should be the cube of the inputted integer. For example, 3 in -> 27 out.

Thanks in advance!

3 个答案:

答案 0 :(得分:0)

You are trying to return a local reference that cannot be accessed outside of it declaration scope. You need to allocate memory on the heap using the new keyword and not the stack. Check out the difference here.

int* cube(int number)
{
    return new int (number * number * number);
}

Note that I pass number by value and not by reference as there is no obvious speed performance/need to modify the value of the passed in variable. I also do not bother making an intermediate variable, newResult, as the result of new can just be returned. Do keep in mind that if I were to create an intermediate value newResult, it would be of type int*. Do NOT forget to give back the memory when you no longer need the result. Use the delete keyword to deallocate the memory. All of that being said, do not bother returning a pointer as use of heap is slow and manual memory allocation is messy.

Below is what you should use.

int cube(int number)
{
    return number * number * number;
}

Creating a static and global variables should be used with care as they will use more memory.

Passing a primitive variable by reference is also not advisable read Reasons to not pass simple types by reference?.

void cube3(int &number)
{
   number = (number) * (number) * (number);
}

答案 1 :(得分:0)

result is a local variable and allocated on the stack. When that function ends that memory owned by result is no longer valid. Therefore any pointer to that memory is not valid.

For a function that simple there's no need to bother with pointers. Just return a copy of the result by value.

int cube(int *number)
{
    int result = (*number) * (*number) * (*number);
    return result;
}

答案 2 :(得分:0)

It is about a scope of variables.

1. Global variable.

The 'result' is a local variable that can not be accessed or used outside { and } block. You can use it by a global variable.

The global variables are declared at the top of the program outside all of the functions or blocks.

static int result;
void cube(int *number)
{
    result = (*number) * (*number) * (*number);
}

In C, you can pass parameters to a function by pointers.

void cube2(int *number)
{
    *number = (*number) * (*number) * (*number);
}

2. Pass by reference.

In C++, you can pass parameters to a function either by pointers(cube2) or by reference code blow:

void cube3(int &number)
{
   number = (number) * (number) * (number);
}

In a main function:

int main()
{
    int input_num, n;
    std::cout << "Enter an int: ";
    std::cin >> n;

    input_num = n;
    cube(&n);

    std::cout << "Cube of " << input_num << " is " << result << std::endl;

    cube2(&n);
    std::cout << "Cube of " << input_num << " is " << n << std::endl;

    n = input_num;
    cube3(n);
    std::cout << "Cube of " << input_num << " is " << n << std::endl;

    return 0;
}