Thread: dynamic memory allocation using pointer to pointers

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    36

    dynamic memory allocation using pointer to pointers

    the question is; Write a program that prints out the memory addresses of each element in a two-dimensionalarray. Check to see if the values printed out make sense to you based on the way I explained it
    before.

    Below is the code I have done. I am having problems printing the "-" sign to keep formatting with the board when the user enter in different dimensions other than [4][4].

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    void printTable (int x, int y) {
        int **p_p_twoDimension = new int* [y];
    
    
        for (int i = 0; i < y; i++) {
            p_p_twoDimension[i] = new int [x];
        }
        for (int i = 0; i < y; i++) {
            for (int j = 0; j < x; j++) {
                cout << &p_p_twoDimension[i][j];
                if (j!= x - 1) {
                    cout << " | ";
                }
            }
            cout << endl;
            if (i!= y - 1) {
                  for (int k = 0; k < (x*10)+1; ++k) {
                    cout << "-";
                  }
                  cout << endl;
            }
        }
    }
    
    
    void freeTable (int **p_p_twoDimension, int y) {
        for (int i=0; i < y; i++) {
            delete [] p_p_twoDimension;
        }
    }
    
    
    int main()
    {
        int x;
        int y;
        cout.flush() << "Please Enter the two dimensions you require" << endl;
        cin >> x >> y;
        int **p_p_twoDimension;
        printTable(x, y);
        freeTable(p_p_twoDimension, y);
        return 0;
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    I ran it. I'm not seeing a problem: If you make the table big enough, the window will wrap the text, but you can't do anything about that.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    freeTable(p_p_twoDimension, y);
    This call doesn't make sense. p_p_twoDimension is an uninitialized pointer
    The array you're printing is local printTable() and is lost after return from that function -> leak

    Kurt

  4. #4
    Registered User
    Join Date
    Mar 2013
    Posts
    36
    hi thanks think this rectified now.

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    
    
    void printTable (int x, int y) {
        int **p_p_twoDimension = new int* [y];
    
    
        for (int i = 0; i < y; i++) {
            p_p_twoDimension[i] = new int [x];
        }
        for (int i = 0; i < y; i++) {
            for (int j = 0; j < x; j++) {
                cout << &p_p_twoDimension[i][j];
                if (j!= x - 1) {
                    cout << " | ";
                }
            }
            cout << endl;
            if (i!= y - 1) {
                  for (int k = 0; k < (x*10)+1; ++k) {
                    cout << "-";
                  }
                  cout << endl;
            }
        }
        for (int i=0; i < y; i++) {
            delete [] p_p_twoDimension;
    
    
        }
    }
    
    
    int main()
    {
        int x;
        int y;
        cout.flush() << "Please Enter the two dimensions you require" << endl;
        cin >> x >> y;
        //int **p_p_twoDimension;
        printTable(x, y);
        //freeTable(p_p_twoDimension, y);
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Still leaking. You have to delete all the pointers to int as well.
    Kurt

  6. #6
    Registered User
    Join Date
    Mar 2013
    Posts
    36
    Sorry just noticed that just after posting it.

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    
    
    void printTable (int x, int y) {
        int **p_p_twoDimension = new int* [y];
    
    
        for (int i = 0; i < y; i++) {
            p_p_twoDimension[i] = new int [x];
        }
        for (int i = 0; i < y; i++) {
            for (int j = 0; j < x; j++) {
                cout << &p_p_twoDimension[i][j];
                if (j!= x - 1) {
                    cout << " | ";
                }
            }
            cout << endl;
            if (i!= y - 1) {
                  for (int k = 0; k < (x*10)+1; ++k) {
                    cout << "-";
                  }
                  cout << endl;
            }
        }
        for (int i=0; i < y; i++) {
            delete [] p_p_twoDimension[i];
    
    
        }
    }
    
    
    int main()
    {
        int x;
        int y;
        cout.flush() << "Please Enter the two dimensions you require" << endl;
        cin >> x >> y;
        printTable(x, y);
        return 0;
    }

  7. #7
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    now only a single call to
    Code:
    delete [] p_p_twoDimension;
    is missing
    Kurt

  8. #8
    Registered User
    Join Date
    Mar 2013
    Posts
    36
    I thought that I only had to release the memory once? Is that not the case?

    Below is what I have adjusted. Thanks for the help.

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    
    
    void printTable (int x, int y) {
        int **p_p_twoDimension = new int* [y];
    
    
        for (int i = 0; i < y; i++) {
            p_p_twoDimension[i] = new int [x];
        }
        for (int i = 0; i < y; i++) {
            for (int j = 0; j < x; j++) {
                cout << &p_p_twoDimension[i][j];
                if (j!= x - 1) {
                    cout << " | ";
                }
            }
            cout << endl;
            if (i!= y - 1) {
                  for (int k = 0; k < (x*10)+1; ++k) {
                    cout << "-";
                  }
                  cout << endl;
            }
        }
        for (int i=0; i < y; i++) {
            delete [] p_p_twoDimension[i];
        }
        delete p_p_twoDimension;
    }
    
    
    int main()
    {
        int x;
        int y;
        cout.flush() << "Please Enter the two dimensions you require" << endl;
        cin >> x >> y;
        printTable(x, y);
        return 0;
    }

  9. #9
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    It's ok now. You need a call to delete for every call to new.
    Kurt

  10. #10
    Registered User
    Join Date
    Mar 2013
    Posts
    36
    Thanks. Think I need to go back to some old problems and sort them out!

  11. #11
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by mp252 View Post
    Write a program that prints out the memory addresses of each element in a two-dimensional array.
    dynamic memory allocation using pointer to pointers
    Did the problem statement mention that you should use pointer to pointers? Technically that's not a 2 dimensional array. Example of two dimensional array (note, NROW could be a variable, but NCOL needs to be a constant).

    Code:
    #define NROW 4
    #define NCOL 5
    
        int (*prow)[NCOL] = new int[NROW][NCOL];     // pointer to first row of two dimensional array
    Last edited by rcgldr; 06-22-2013 at 04:20 PM.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Also just throwing in that the usual way to use a 2D array in C++ is to do:

    std::vector<std::vector<T>> vector(N, std::vector<T>(M)); // <--- dynamic array; N and M must be known at runtime

    std::array<std::array<T, M>, N> array; // <--- static array; N and M must be known at compile time. Also requires C+11.

    Knowing how to allocate 2d arrays with pointers is all fine and dandy, but know that in production code - and if you have a choice - you should prefer using a vector or array construct. It's safer, faster and you don't have to deallocate the memory manually.
    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.

  13. #13
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by Elysia View Post
    Also just throwing in that the usual way to use a 2D array in C++ is to do ... vector of vectors
    I'm wondering if this specific problem statement is asking to create a true 2D array, where the memory for the entire 2D array is contiguous and only contains values (no instances of pointers within the array).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers and Dynamic memory allocation help needed
    By dave_the_bear10 in forum C Programming
    Replies: 3
    Last Post: 11-09-2011, 04:23 PM
  2. Dynamic Memory Allocation
    By slash.hack in forum C Programming
    Replies: 9
    Last Post: 09-30-2011, 04:31 AM
  3. Link List,Structures,Dynamic Memory Allocation,Pointers
    By Tanuj_Tanmay in forum C Programming
    Replies: 2
    Last Post: 04-24-2009, 08:55 PM
  4. Dynamic Memory Allocation?
    By motocross95 in forum C++ Programming
    Replies: 11
    Last Post: 12-03-2002, 08:52 PM
  5. dynamic memory allocation and returning pointers
    By sballew in forum C Programming
    Replies: 7
    Last Post: 11-03-2001, 03:21 PM

Tags for this Thread