2
#include<iostream>
using namespace std;

int *Arr(int y,int size){
int arg[size];
for(int i=size-1;i>=0;i--){
    arg[i]=y%10;
    y=y/10;
}
return arg;
}
int main(){
int *p=Arr(2587,4);
for(int j=0;j<4;j++){
  cout<<p[j]<<"   ";
}
return 0;
}

> Blockquote

I dont why this isn't working ...I'm trying to back an array but the problem is in the second digits.Can somebody help ;) thanks

2 Answers2

1

The problem is you are putting your result into a local array that is destroyed when the function ends. You need to dynamicaly allocate the array so that its life-span is not limited to the function it was created in:

#include<iostream>
using namespace std;

int *Arr(int y, int size)
{
    // This local array will be destroyed when the function ends
    // int arg[size];

    // Do this instead: allocate non-local memory
    int* arg = new int[size];

    for(int i = size - 1; i >= 0; i--)
    {
        arg[i] = y % 10;
        y = y / 10;
    }
    return arg;
}

int main()
{
    int *p = Arr(2587, 4);
    for(int j = 0; j < 4; j++)
    {
        cout << p[j] << "   ";
    }

    // You need to manually free the non-local memory
    delete[] p; // free memory

    return 0;
}

NOTE:

Allocating dynamic memory using new is to be avoided if possible. You may want to study up on smart pointers for managing it.

Also, in real C++ code, you would use a container like std::vector<int> rather than a builtin array

Galik
  • 47,303
  • 4
  • 80
  • 117
  • Standard containers manage dynamically allocated arrays (or collections of multiple objects, more generally), whereas smart pointers generally manage the lifetime of a single dynamically allocated object (not an array). Making a smart pointer manage a dynamically allocated array causes undefined behaviour when the smart pointer cleans up. – Peter Apr 05 '15 at 23:53
  • @Peter `std::unique_ptr` handles arrays using a specialization: http://stackoverflow.com/a/6713515/3807729 [std::unique_ptr](http://en.cppreference.com/w/cpp/memory/unique_ptr) – Galik Apr 05 '15 at 23:58
  • Also `std::shared_ptr` can handle arrays if provided with a custom deleter: http://stackoverflow.com/a/13062069/3807729 – Galik Apr 06 '15 at 00:07
  • Fair enough. That's new to me, so thanks for that. Albeit, from the link, a unique_ptr will not handle an array, but a unique_ptr will. – Peter Apr 06 '15 at 00:11
0

Of course it is not working.

At best, the behaviour is undefined, since Arg() is returning the address of a local variable (arg) that no longer exists for main(). main() uses that returned address when it is not the address of anything that exists as far as your program is concerned.

There is also the incidental problem that int arg[size], where size is not fixed at compile time, is not valid C++. Depending on how exacting your compiler is (some C++ compilers reject constructs that are not valid C++, but others accept extensions like this) your code will not even compile successfully.

To fix the problem, have your function return a std::vector<int> (vector is templated container defined in the standard header <vector>). Then all your function needs to do is add the values to a local vector, which CAN be returned safely by value to the caller.

If you do it right, you won't even need to use a pointer anywhere in your code.

Peter
  • 35,646
  • 4
  • 32
  • 74