-7

Why the below gives different Outputs?

 #include<iostream>

 using namespace std;

 int* sqcb(int n)
 {
     int a[3];
     a[0]=n*n;
     a[1]=n*n*n;
     a[2]=786;
     return a;  
 }

 int main()
 {
     int a,c;int const *b;
     cout<<"Enter an Integer:";
     cin>>a;

     b=sqcb(a);

     cout<<*(b)<<"\t"<<*(b+1)<<"\t"<<*(b+2)<<"\t"; // <- this

     cout<<*(b)<<"\t";   // <- and these
     cout<<*(b+1)<<"\t"; // 
     cout<<*(b+2)<<"\t"; // 
 }
durron597
  • 31,968
  • 17
  • 99
  • 158
Saj Gur
  • 5
  • 2
  • @KirilKirov are the line numbers unchanged after your edit? – Wolf Feb 28 '14 at 10:04
  • 1
    @Wolf - ou, sorry, will revert.. – Kiril Kirov Feb 28 '14 at 10:05
  • No, don't revert, just fix the line numbers – LordAro Feb 28 '14 at 10:06
  • 3
    Basics of debugging: Input, observed output, expected output. Proper indenting, some whitespaces and comments about the intention of your source would help as well. – DevSolar Feb 28 '14 at 10:06
  • possible duplicate of [Returning pointer to a local structure](http://stackoverflow.com/questions/1475635/returning-pointer-to-a-local-structure) – jerry Feb 28 '14 at 10:29
  • That was not the duplicate I marked. http://stackoverflow.com/questions/10381921/cast-an-array-to-pointer-in-function-return/10381961#10381961 (or maybe http://stackoverflow.com/questions/2320551/return-pointer-to-data-declared-in-function) is a better fit. – jerry Feb 28 '14 at 10:32

4 Answers4

4
int* sqcb(int n)
{
  int a[3];
  a[0]=n*n;
  a[1]=n*n*n;
  a[2]=786;
  return a;  
}

You are returning a pointer to a local variable, which is naughty.

Marc Claesen
  • 16,778
  • 6
  • 27
  • 62
1

You're returning a pointer to a local variable.
You're not allowed to do that.
The program is undefined, so anything can happen. Through bad luck, the program doesn't crash for you.
Most likely, whatever data used to be where b points gets overwritten by the streaming function calls.

If possible, increase the warning level of your compiler, and treat warnings as errors.

If you want to return a collection from a function, you should investigate std::vector and std::array.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
1

Your code in sqcb causes undefined behavior: your return the address of the array variable a which gets invalid after the function returns. You may change your sqcbfunction in one of the following ways as to get the equal outputs you expect.

Using a global variable

int g_a[3]; // global variable, this clutters the global namespace and
            // is accessible (and therefore mis-usable) from everywhere and
            // a lot of more problems

int* sqcb(int n)
{
    g_a[0]=n*n;
    g_a[1]=n*n*n;
    g_a[2]=786;
    return g_a;
}

Switch to a static variable

int* sqcb(int n)
{
    static int a[3]; // static variable
                     // this may causes problems in multi-threaded environments
    a[0]=n*n;
    a[1]=n*n*n;
    a[2]=786;
    return a;
}

But I'd prefer rethinking the problem and change the whole program.

Wolf
  • 9,679
  • 7
  • 62
  • 108
1

Array int a[3]; is a local array of function sqcb. After exiting the function the memory used by the function is relaesed and can be used for other requirements (for example it can be used by operator functions of stream std::cout). So your program has undefined behaviour because the function returns a pointer to this released memory,

You could get well-formed program if you would define this array as having static storage duration. For example

int* sqcb(int n)
 {
     static int a[3];
     a[0]=n*n;
     a[1]=n*n*n;
     a[2]=786;
     return a;  
 }
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335