在运行时从指针到指针数组中删除字符串时出错

Error at runtime while deleting a string from a pointer to pointer array

本文关键字:指针 字符串 删除 出错 数组 运行时      更新时间:2023-10-16

我试图使用3函数在指针到指针数组的末尾添加一个字符串,或者在选定的位置插入字符串,然后删除选定的元素。

我使用的第一个函数是在指向指针的指针末尾添加字符串:

char **add(char **pp, int size, char *str)
{
    if (size == 0)
    {
        pp = new char *[size+1];
    }
    else
    {
        char **temp = new char *[size+1];
        for (int i = 0; i < size; i++)
        {
            temp[i] = pp[i];
        }
        delete[]pp;
        pp = temp; 
    }
    pp[size] = new char[strlen(str) + 1];
    strcpy(pp[size], str);
    return pp;
}

我用来在选定位置插入字符串的第二个函数:

char **InsPtr(char **pp, int size, int ncell, char *str)
{
    char**temp = new char *[size+1];  //добавить новый элемент [size+1]
    for (int i = 0, j = 0; j < size + 1; j++)
    {
        if (j != ncell)
        {
            temp[j] = pp[i];
            i++;
        }
        else
        {
            temp[j]=new char[ strlen(str)+1];
            strcpy(temp[j], str);
        }
    }
    delete[] pp;
    pp = temp;
    return pp;
}

这是我用来删除我选择的任何字符串的函数:

char **DelPtr(char **pp, int size, int ncell)
{
    char**temp = new char*[size-1];
    for (int i = 0, j = 0; i < size; i++)
    {
        if (i != ncell)
        {
            temp[j] = pp[i];
            j++;
        }
    }
    delete[]pp[ncell];
    pp[ncell] = 0;
    delete[] pp;
    pp = temp;
    return pp;
} 

我用**add(char **pp, int size, char *str)向数组中添加了4个字符串,然后用**InsPtr(char **pp, int size, int ncell, char *str)在选定的位置插入一个字符串,或者用**DelPtr(char **pp, int size, int ncell)删除一个字符串。它工作正常,没有任何错误。

但是当我使用**InsPtr(char **pp, int size, int ncell, char *str)时,在这个函数之后,我使用**DelPtr(char **pp, int size, int ncell)我在运行时出错。

以下是主要功能:

void main()
{   
    int size = 0; 
    char **pp = 0;
    pp = add(pp, size, "1111");
    size++;
    pp = add(pp, size, "2222");
    size++;
    pp = add(pp, size, "3333");
    size++;
    pp = add(pp, size, "4444");
    size++;
    int insert=2,DelS= 2;
    for (int i = 0; i < size; i++)
    {
        cout << *(pp + i) << endl;
    }
    pp = InsPtr(pp, size, insert, "natasha");
    cout << endl;
    for (int i = 0; i < size + 1; i++)
    {
        cout << *(pp + i) << endl;
    }
    pp = DelPtr(pp, size, DelS);
    cout << endl;
    for (int i = 0; i < size ; i++)
    {
        cout << *(pp + i) << endl;
    }
    system("pause");
}

以下是我在运行时出现错误之前得到的结果:

1111
2222
3333
4444

在第二个索引"natasha"中插入:

1111
2222
natasha
3333
4444

删除第二个索引"natasha":

1111
2222
3333

只需在pp = InsPtr(pp, size, insert, "natasha");之后添加size++;pp = DelPtr(pp, size, DelS);之后的size--;:)

像这样:

void main()
{   
    int size = 0; 
    char **pp = 0;
    pp = add(pp, size, "1111");
    size++;
    pp = add(pp, size, "2222");
    size++;
    pp = add(pp, size, "3333");
    size++;
    pp = add(pp, size, "4444");
    size++;
    int insert=2,DelS= 2;
    for (int i = 0; i < size; i++)
    {
    cout << *(pp + i) << endl;
    }
     pp = InsPtr(pp, size, insert, "natasha");
     cout << endl;
    for (int i = 0; i < size + 1; i++)
    {
    cout << *(pp + i) << endl;
    }
    size++;
    pp = DelPtr(pp, size, DelS);
    cout << endl;
    size--;
    for (int i = 0; i < size ; i++)
    {
        cout << *(pp + i) << endl;
    }
    system("pause");
}