deleting pointer array of pointer array

This is a discussion on deleting pointer array of pointer array within the C++ Programming forums, part of the General Programming Boards category; my program tends to crash when i try to delete this array...heres the basic setup EDIT: my program SOMETIMES crashes..it ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    266

    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..it crashes sometimes..and sometimes it runs smoothly

    very weird stuff!
    what is wrong?
    Code:
    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;
    
    //MORE CODE HERE
    
    //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 10:38 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,753
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Posts
    1,628
    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).
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,623
    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21