user2804865
user2804865

Reputation: 1064

Deleting from a dynamically allocated array

So I have a 2D array

char **m_data

This array has a width of width and the height is given by a vector called m_heigths.

// Remove blank space where necessary
// Iterate through every row
for (int x=0; x<m_width; ++x){
// count number of spaces
    int spaces=0;
    // iterate through the given row
    for (int y=0; y<m_heigths[x]; ++y){
    // if space is occupied by a black space increment count
        if (m_data[x][y]==' '){
            ++spaces;
        }
    }
    // check if entire column is just a bunch of blanks
    if (spaces==m_heigths[x]){
        // get rid of blanks
        delete [] m_data[x];
    }
}

So I want to look for a column that is just a bunch of blank spaces and delete it. but this does not seemed to work, the blank spaces stay there. Could anyone help me?

Upvotes: 0

Views: 70

Answers (1)

Michael Albers
Michael Albers

Reputation: 3779

delete only releases the allocated memory. To truely delete the row you'll need to copy (in this case, just copy the pointers) all rows below up by one after you call delete. Like Hayden said in the comments it'll probably be easier to use an STL container.

Upvotes: 2

Related Questions