pointer corruption

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nair SL

    pointer corruption

    I want to understand whether this following piece of code is giving me
    junk result becoz I'm trying to access values out of scope?

    int* array_display() {
    int array1[4] = {11,22,33,44};
    int *qq = array1;
    return qq;
    }

    int main()
    {
    int *pp;
    pp = array_display() ;

    for(i=0; i<4; ++i,++pp) {
    int temp = *(pp); //case 1
    // int temp = *(pp+1); //case 2 returns 22 first and
    subsequent junk
    cout<<"pp array ->- "<<temp<<en dl;
    }
    return 0;
    }
    expected output : 11 22 33 44
    but I'm hitting ony the first access point. Rest is giving me junk.
    The point is if I can access any values by incrementing pointer (as in
    the second case), why I could not access them a second time?

    Any help will be appreciated.
    Thanks in advance...
  • Morya

    #2
    Re: pointer corruption

    On Nov 10, 1:48 pm, nair SL <anoop...@gmail .comwrote:
    I want to understand whether this following piece of code is giving me
    junk result becoz I'm trying to access values out of scope?
    >
    int* array_display() {
            int array1[4] = {11,22,33,44};
            int *qq = array1;
            return qq;
    >
    }
    >
    int main()
    {
            int *pp;
            pp = array_display() ;
    >
            for(i=0; i<4; ++i,++pp) {
                 int temp = *(pp); //case 1
                 // int temp = *(pp+1); //case 2 returns 22 first and
    subsequent junk
                    cout<<"pp array ->- "<<temp<<en dl;
            }
            return 0;}
    >
    expected output : 11 22 33 44
    but I'm hitting ony the first access point. Rest is giving me junk.
    The point is if I can access any values by incrementing pointer (as in
    the second case), why I could not access them a second time?
    >
    Any help will be appreciated.
    Thanks in advance...
    IMHO, array is local to array_display() which means it runs out of
    scope when array_display() returns.
    HTH.

    ~Mo

    Comment

    • Andrey Tarasevich

      #3
      Re: pointer corruption

      nair SL wrote:
      I want to understand whether this following piece of code is giving me
      junk result becoz I'm trying to access values out of scope?
      >
      int* array_display() {
      int array1[4] = {11,22,33,44};
      int *qq = array1;
      return qq;
      }
      >
      int main()
      {
      int *pp;
      pp = array_display() ;
      >
      for(i=0; i<4; ++i,++pp) {
      int temp = *(pp); //case 1
      // int temp = *(pp+1); //case 2 returns 22 first and
      subsequent junk
      cout<<"pp array ->- "<<temp<<en dl;
      }
      return 0;
      }
      expected output : 11 22 33 44
      but I'm hitting ony the first access point. Rest is giving me junk.
      The point is if I can access any values by incrementing pointer (as in
      the second case), why I could not access them a second time?

      'array1' is a local (automatic) array in 'array_display' function. Once
      this function finishes working they array is gone. It no longer exists.
      It cannot be accessed regardless of how you try to access it. What you
      do in your code after calling 'array_display' produces undefined behavior.

      --
      Best regards,
      Andrey Tarasevich

      Comment

      • Morya

        #4
        Re: pointer corruption

        On Nov 10, 2:02 pm, Morya <mda...@gmail.c omwrote:
        On Nov 10, 1:48 pm, nair SL <anoop...@gmail .comwrote:
        >
        >
        >
        I want to understand whether this following piece of code is giving me
        junk result becoz I'm trying to access values out of scope?
        >
        int* array_display() {
                int array1[4] = {11,22,33,44};
                int *qq = array1;
                return qq;
        >
        }
        >
        int main()
        {
                int *pp;
                pp = array_display() ;
        >
                for(i=0; i<4; ++i,++pp) {
                     int temp = *(pp); //case 1
                     // int temp = *(pp+1); //case 2 returns 22first and
        subsequent junk
                        cout<<"pp array ->- "<<temp<<en dl;
                }
                return 0;}
        >
        expected output : 11 22 33 44
        but I'm hitting ony the first access point. Rest is giving me junk.
        The point is if I can access any values by incrementing pointer (as in
        the second case), why I could not access them a second time?
        >
        Any help will be appreciated.
        Thanks in advance...
        >
        IMHO, array is local to array_display() which means it runs out of
        scope when array_display() returns.
        HTH.
        >
        ~Mo
        On a second note i see that you are not displaying the array at all in
        the array_display() . But you are trying to allocate something. Might
        want to consider using a reference to a pointer. Your code might look
        something like this:

        void allocate_array( int*& qq)
        {
        qq = new int[4]; // google around for how to initialize.
        }

        int main()
        {
        int * array;
        allocate_array( array);
        for(i=0;i<4;++i ) {
        cout << array[i];
        }
        delete [] array;
        }

        HTH.
        ~Mo

        Comment

        • Thomas J. Gritzan

          #5
          Re: pointer corruption

          Morya wrote:
          [...returning pointer to local array...]
          On a second note i see that you are not displaying the array at all in
          the array_display() . But you are trying to allocate something. Might
          want to consider using a reference to a pointer. Your code might look
          something like this:
          >
          void allocate_array( int*& qq)
          {
          qq = new int[4]; // google around for how to initialize.
          }
          >
          int main()
          {
          int * array;
          allocate_array( array);
          for(i=0;i<4;++i ) {
          cout << array[i];
          }
          delete [] array;
          }
          Please don't use new[]! It's a waiting memory leak. There are much
          better tools around.

          std::vector<int filled_array()
          {
          int array1[4] = {11,22,33,44};
          // use begin()/end() templates if you like
          std::vector<int arr(array1, array1 + 4);
          return arr;
          }

          int main()
          {
          std::vector<int array = filled_array();
          for(size_t i = 0; i<array.size() ; ++i)
          std::cout << array[i];
          }

          --
          Thomas

          Comment

          • gw7rib@aol.com

            #6
            Re: pointer corruption

            On 10 Nov, 08:48, nair SL <anoop...@gmail .comwrote:
            I want to understand whether this following piece of code is giving me
            junk result becoz I'm trying to access values out of scope?
            >
            int* array_display() {
                    int array1[4] = {11,22,33,44};
                    int *qq = array1;
                    return qq;
            >
            }
            >
            int main()
            {
                    int *pp;
                    pp = array_display() ;
            >
                    for(i=0; i<4; ++i,++pp) {
                         int temp = *(pp); //case 1
                         // int temp = *(pp+1); //case 2 returns 22 first and
            subsequent junk
                            cout<<"pp array ->- "<<temp<<en dl;
                    }
                    return 0;}
            I think you're right about the problem, but you are using slightly the
            wrong name for it.

            As the program stands, array1 disappears when the function
            array_display finishes. So when you try to read it in main, it's gone
            before you finish.

            You can fix this problem by putting the word "static" in front of the
            "int array1". This will cause the array to exist all the time the
            program is running, and so (if you also add "int i;" to main, and a
            suitable header) the program will work.

            The property of how long a variable hangs around for is called its
            "lifetime". In your program as stated, the lifetime of the array was
            while array_display was running, with my changes, the lifetime of the
            array is the time the program is running.

            The "scope" of a variable is the places in the program where you can
            refer to it by name. In your program, and indeed in my modification of
            it, the scope of array1 is the routine array_display. You can't refer
            to the array by the name "array1", or by the name "qq", in main. But
            if the lifetime of the array is still running, you can refer to it
            using another variable which has the same value - in this case, using
            pp.

            Hope that helps.
            Paul.

            Comment

            • nair SL

              #7
              Re: pointer corruption

              On Nov 11, 1:03 am, gw7...@aol.com wrote:
              On 10 Nov, 08:48, nair SL <anoop...@gmail .comwrote:
              >
              >
              >
              I want to understand whether this following piece of code is giving me
              junk result becoz I'm trying to access values out of scope?
              >
              int* array_display() {
                      int array1[4] = {11,22,33,44};
                      int *qq = array1;
                      return qq;
              >
              }
              >
              int main()
              {
                      int *pp;
                      pp = array_display() ;
              >
                      for(i=0; i<4; ++i,++pp) {
                           int temp = *(pp); //case 1
                           // int temp = *(pp+1); //case 2 returns 22first and
              subsequent junk
                              cout<<"pp array ->- "<<temp<<en dl;
                      }
                      return 0;}
              >
              I think you're right about the problem, but you are using slightly the
              wrong name for it.
              >
              As the program stands, array1 disappears when the function
              array_display finishes. So when you try to read it in main, it's gone
              before you finish.
              >
              You can fix this problem by putting the word "static" in front of the
              "int array1". This will cause the array to exist all the time the
              program is running, and so (if you also add "int i;" to main, and a
              suitable header) the program will work.
              >
              The property of how long a variable hangs around for is called its
              "lifetime". In your program as stated, the lifetime of the array was
              while array_display was running, with my changes, the lifetime of the
              array is the time the program is running.
              >
              The "scope" of a variable is the places in the program where you can
              refer to it by name. In your program, and indeed in my modification of
              it, the scope of array1 is the routine array_display. You can't refer
              to the array by the name "array1", or by the name "qq", in main. But
              if the lifetime of the array is still running, you can refer to it
              using another variable which has the same value - in this case, using
              pp.
              >
              Hope that helps.
              Paul.
              Thanks for all the Tips.
              That helped me a lot.

              Comment

              Working...