2
#include <iostream>
#include <string>
#include <utility>
using namespace std;
string num1="123456789123456789";
std::pair<int*,int*> cpy(){
    int  a[(num1.size()%9==0)? num1.size()/9 : num1.size()/9+1];
    int  b[(num1.size()%9==0)? num1.size()/9 : num1.size()/9+1];
   return make_pair(a,b);
}
int main(void){
   return 0;
}
-------------------------------------------------------
//if by this style, it can be compiled
std::pair<int*,int*> cpy(){
const int N=5;
int  a[5];
int  b[5];
return make_pair(a,b);

}

I am writing a program to calculate Big Number such as 19933231289234783278, So I need split the number using 1 000 000 000 system

Why can't return a pair by this way?

markliang
  • 41
  • 1
  • 7
  • Yeah g++ stoi.cpp -o stoi -std=c++11 – markliang Aug 25 '15 at 07:12
  • 7
    Be sure to read [this](http://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope?lq=1) sometime. You're attempting to return dangling pointers. – chris Aug 25 '15 at 07:12
  • you are using variable length arrays (an extension) which seem to be not supported by templates – Piotr Skotnicki Aug 25 '15 at 07:13
  • Thanks, Suddenly I rember that C++ primer mentioned it – markliang Aug 25 '15 at 07:15
  • 2
    In C++11 `make_pair` takes forwarding references, so for an array `make_pair` tries to deduce its size, which can't happen for Variable Length Arrays. That's why it works in C++03 (decay takes place when calling `make_pair`), and doesn't work in C++11 (decay is about to take place inside `make_pair`) – Piotr Skotnicki Aug 25 '15 at 07:17

2 Answers2

3

You can't return a pair this way, because you are passing wrong types. In this case, array doesn't decay to pointer.

If you change the last line in the function to this :

return make_pair(&a[0],&b[0]);

it will compile, but it will still not work, as you are returning pointers to arrays, which are destroyed, once the function cpy() ends.

By the way, variable lenghts array are not standard c++.

BЈовић
  • 62,405
  • 41
  • 173
  • 273
1

You are trying to return pointer to a temporary value. As result the pointers will be point to a garbage.

Also you trying to cast string with decimal notation to integer. You should return the pair of integers, which will be obtained by parsing the string at the numbers.

You can see the link below. http://interactivepython.org/runestone/static/pythonds/BasicDS/ConvertingDecimalNumberstoBinaryNumbers.html