1

Possible Duplicate:
Can a local variable’s memory be accessed outside its scope?

#include <iostream>
#include <string>

using namespace std;

char* reverse(char* input) 
{
    char b[11];

    for (int i=0; i<11; i++)    {b[10-i]=input[i];}
    for (int u=0; u<11; u++)    {cout<<b[u];}
    cout<<endl;
    return &b[0];
}

int main ()
{
     char ali[]="ali is good";
     char *a=&ali[0];
     char *b=reverse(a);

     for (int i=0; i<11; b++, i++)  
        {
            cout<<*b+i<<endl;
        }
     cout<<endl;
     system("pause");
     return 0;
}

This is a simple program to reverse a string, it works well when you print the result in the function, but when passing the pointer of the string to Main then printing it, it prints garbage-like things.

Why is it strange? because if I print *b+2 or *b+5 or whatever const I like, it works well, but if I replace the const with an int in a loop like *b+i then it does not work!!

P.S. this is not a HW!! I "was" a longtime programmer, shame on me for forgetting all of this.

Any ideas?

Community
  • 1
  • 1
ali8
  • 23
  • 4

4 Answers4

2

The variables declared inside a function are automatic storage duration objects and go ou tof scope at the end of a function (and thus are no longer valid). To allocate storage that lives longer than a function you need dynamic storage duration objects (these are allocated via new):

char *b = new char[11];
Martin York
  • 257,169
  • 86
  • 333
  • 562
Mihail Burduja
  • 3,196
  • 2
  • 22
  • 28
  • Yes "Mihail Burduja", that solved it! And also there were some "Typos" like incrementing b and i while I should have incremented only b, but this is irrelevant. – ali8 Nov 03 '12 at 16:38
  • Shame on me for forgetting all these elementary topics! Thanks everyone. – ali8 Nov 03 '12 at 16:42
1

You should never return a pointer to a local variable, that why you see the error.

The local variables are allocated on the stack. When the variable goes out of scope, the memory is de-allocated.

Now you are trying to access the variable which has been de-allocated because it has gone out of scope(and hence removed from stack)

Also

 for (int i=0; i<11; b++, i++)  
                        {
                            cout<<*(b+i)<<endl;
                        }

Why are incrementing both b and i?

You should increment just i and the not the base pointer address(b)

In summary this is the program you should use

#include<iostream>
   using namespace std;

             void reverse(char* input,char * output) 
             {


                for (int i=0; i<11; i++)    {output[10-i]=input[i];}
                for (int u=0; u<11; u++)    {cout<<output[u];}
                cout<<endl;

            }

             int main ()
             {
                 char ali[]="ali is good";
                 char *a=&ali[0];
                 char *b=(char *)malloc(sizeof(ali));
                 reverse(a,b);

                 for (int i=0; i<11;  i++)  
                    {
                        cout<<*(b+i)<<endl;
                    }
                 cout<<endl;
                 system("pause");
                 return 0;
             }
Desert Ice
  • 4,461
  • 5
  • 31
  • 58
0

Last but not least, *b+i means the value of the object pointed to by b raised by i (that is (*b)+i), not *(b+i) as you wish.

jpalecek
  • 47,058
  • 7
  • 102
  • 144
0

try this instead

char* reverse(char* input) 
{
  int len = strlen(input);
  for (int i=0; i<len/2; ++i) 
  {
    char tmp = input[i];
    input[i] = input[len-i-1];
    input[len-i-1] = tmp;
  }
  return input;
}

then you are not returning a local variable but returning the original array with the reversed string.

AndersK
  • 35,813
  • 6
  • 60
  • 86