Thread: Pointers

  1. #1
    Registered User
    Join Date
    May 2013
    Not so far

    Question Pointers

    Hi there,

    I have a question about pointers. I have two pointers: pnArray and pnPtr. The former points to a dynamically allocated array of integers and the latter points to the former. This is my code:

    #include <iostream>
    void print_array(int *pnArray, int nCount)
        using namespace std;
        for(int i=0; i<nCount; ++i)
            cout << *(pnArray+i) << ", ";
        cout << endl;
    int main()
        int *pnArray = new int[5];
        for(int i=0; i<5; ++i)
            pnArray[i] = i+i;
        print_array(pnArray, 5);
        int *pnPtr;
        pnPtr = pnArray;
        print_array(pnPtr, 5);
        delete[] pnArray;
        pnArray = 0;
        print_array(pnPtr, 5);
        return 0;
    After freeing the allocated memory which is being pointed to by pnArray, I try to print what is pnPtr pointing to. I was expecting a crash but to my surprise, pnPtr actually did print what pnArray was pointing to!

    How is this happening? Apologies if the answer is obvious, I'm still learning C++.


  2. #2
    Registered User
    Join Date
    Aug 2005
    You were lucky the freed array was still in memory at the same location, propably because you never new'ed any new data after the delete. Using a deleted object gives undefined behavour. Anything can happen.


  3. #3
    Join Date
    Oct 2007
    Inside my computer
    Trying to use freed memory is undefined behaviour. The standard doesn't say what happens.
    For a more realistic explanation, most operating systems don't always block access to freed memory due to performance reasons.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    May 2013
    Not so far
    Oh I see. Thanks for the quick response!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. size of struct with pointers and function pointers
    By sdsjohnny in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 05:19 AM
  2. Storing function pointers in generic pointers
    By Boxknife in forum C Programming
    Replies: 6
    Last Post: 08-01-2009, 01:33 PM
  3. Variable pointers and function pointers
    By Luciferek in forum C++ Programming
    Replies: 11
    Last Post: 08-02-2008, 02:04 AM
  4. Pointers to objects -- passing and returning pointers
    By 1veedo in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2008, 11:42 AM
  5. weak pointers and use_count smart pointers
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 07-29-2006, 07:54 AM

Tags for this Thread