Thread: deleting pointer array of pointer array

  1. #1
    Registered User
    Join Date
    Aug 2005

    deleting pointer array of pointer array

    my program tends to crash when i try to delete this array...heres the basic setup

    EDIT: my program SOMETIMES crashes sometimes..and sometimes it runs smoothly

    very weird stuff!
    what is wrong?
    int ** f;
    int solve( int n, int capacity)
    //create array
    const int rows = n;
    const int cols = capacity;
    f = new int*[rows];
    for(int i = 0; i < rows; i++)
        f[i] = new int[cols];
    for(int j = 0; j < cols; j++)
        for(int i = 0; i < rows; i++)
            f[i][j] = 0;
    //time to delete array of array
    for (int i = 0; i < rows; i++)
      delete [] f[i];  //CRASHES
      //f[i] = null;
    delete [] f;
    //f = null;
    any ideas?
    Last edited by rodrigorules; 02-07-2010 at 11:38 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    The problem probably lies in the code that you did not show, or even in other code, since f is a global variable.

    I suggest that you avoid global variables and use a std::vector<int> or std::vector<std::vector<int> > instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Somewhere, your program is probably experiencing a buffer overflow that's overwriting your f array.
    You need to go over it... carefully. These can be a real pain sometimes (usually only if you poorly wrote something, though).
    I just want to use my computer in a productive manner, not learn how to use it. Elysia
    A year spent in artificial intelligence is enough to make one believe in God. Alan Jay Perlis

  4. #4
    Join Date
    Oct 2007
    Inside my computer
    Buffer overflows are easy to catch with a proper debugger. No need to painfully crawl through it by hand to see if you can spot anything.
    Regardless, you should post the smallest compilable example that demonstrates the problem. That should get you help a little faster.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 6
    Last Post: 05-15-2009, 08:38 AM
  3. Why does C need pointer conversion
    By password636 in forum C Programming
    Replies: 2
    Last Post: 04-10-2009, 07:33 AM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM