1
    void nothing(int* buffer)
    {
        int* temp = new int[5];
        for (int i = 0; i < 5; i++)
        {
            temp[i] = i;
        }
        buffer = temp;
    }

    void main(int argc, char* argv[])
    {
        int* a = new int;
        nothing(a);
        for (int i = 0; i < 5; i++)
        {
            cout << a[i] << endl;
        }
        system("pause");
    }

Why can't I get new address from buffer? I'm trying to pass an array(pointer) to function and modify it inside.

output:

 -842150451
-33686019
-1414812757
-1414812757
0

expected:

0
1
2
3
4
user1046403
  • 555
  • 1
  • 5
  • 11
  • See [this](http://stackoverflow.com/questions/4426474/is-passing-pointer-argument-pass-by-value-in-c) question and corresponding answer. – crayzeewulf Nov 01 '13 at 20:48

6 Answers6

3

You need to pass by pointer to pointer (or as others have pointed out reference to pointer), i.e. int ** and then assign using *buffer = temp and call function using nothing(&a).

If you don't do this, the changes you make to the buffer variable are lost when you leave the function. Think of the pointer variable itself like just any number, where as the thing it points to lives on after the function has ended.

However, I would recommend that you use a std::vector instead:

void nothing(std::vector<int> &buffer) {
    ...
}

int main(int argc, char* argv[])
{
    std::vector<int> a;
    nothing(a);
    for (int i = 0; i < 5; i++)
    {
        cout << a[i] << endl;
    }
    system("pause");
    return 0;
}

Depending on your scenario you might even want to change the nothing function into an initializer style function, i.e. returning an object directly:

std::vector<int> nothing() {
    std::vector<int> temp;
    // fill vector here
    return temp;
}

int main(int argc, char* argv[])
{
    auto a = nothing();
    for (int i = 0; i < 5; i++)
    {
        cout << a[i] << endl;
    }
    system("pause");
    return 0;
}

This is a lot more C++-ish and prevents you from having to handle deleting the array manually.

For actually generating the data into the vector, take a look at Initialization of std::vector<unsigned int> with a list of consecutive unsigned integers .

Community
  • 1
  • 1
villintehaspam
  • 8,540
  • 6
  • 45
  • 76
1

Important prelude: If you can, using vector instead of arrays/pointers. We would all encourage this as much as possible! It's quite possible to program for months on end, writing some quite complex software, and never have to call new or delete and never have to worry about all those nasty C problems.


Just stop using temp!

void nothing(int* buffer)
{
    for (int i = 0; i < 5; i++)
    {
        buffer[i] = i;
    }
}

This will take, as input, a pointer to your array. And it will then write directly to that array.

Your earlier code created a second array. Every time you call new you get a new array. int* temp = new int[5];. For this approach to work you would need to copy data in the temp array to the buffer array. But, you can't copy arrays (easily) in C.

buffer = temp;  // This *doesn't* copy any array

This line did nothing. The two arrays still exist, and no data was copied as a result of this. The local variable called buffer was modified here ; but the change was minor -- buffer used to point at the old array and now it points at the new array. And because buffer was a local variable, it lost all meaning as soon as the function returned (it pointed to non-local data, but the buffer pointer itself is still local.

In shorty, your original nothing function did nothing useful. It created a new array, put some values into it, then it ignored it. The buffer variable inside main was not affected by your function.

Aaron McDaid
  • 26,501
  • 9
  • 66
  • 88
0

a is passed by value to nothing, so changing the copy of it in nothing has no effect on a. You'd have to pass if by reference or, as @villintehaspam says, pass a pointer to the pointer.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
0

"I'm trying to pass an array(pointer) to function and modify it inside"

But what your code does is that it allocates a memory for 1 int, passes a pointer to this memory to your function and your function allocates an array and attempts to assign an address or this newly allocated array to the passed pointer, i.e. it tries to modify the pointer itself.

Try to pass int** instead of int* and let the caller do:

int* a;
nothing(&a);

Also note that in C++ it should be rather rarity than a common practice that a function dynamically allocates some resources and throws a burden in a form of responsibility for freeing these resources (even using specific means, in this case calling delete[]) to the caller. Consider using STL containers such as std::vector or in case you have to stick with C-style arrays, then at least prefer those with automatic storage duration.

LihO
  • 41,190
  • 11
  • 99
  • 167
0

temp is a local variable, declared inside nothing(). This means that the memory for temp is lost when nothing() returns. You can't get the data back from inside a local variable - hence the garbage.

Zach Stark
  • 565
  • 3
  • 8
  • The problem isn't that temp is a local variable, because the value in temp is copied into buffer. The problem is that buffer is *also* a local variable. – Sean Burton Nov 01 '13 at 21:29
0

The pointer buffer is being passed by value (which is the default in c++), and that means a copy is created. You then modify this copy. This being c++, you can pass a reference instead:

void nothing(int*& buffer)

This way, the original object is passed, not a copy.

memo1288
  • 748
  • 1
  • 6
  • 12