c++ - 动态扩展指针数组

标签 c++ dynamic-memory-allocation access-violation dynamic-arrays

<分区>

我正在尝试使用 C++ 构建字典。 字典必须随时动态创建和更新。 例如,假设我的字典中有 5 个单词,我想添加另一个单词,我必须创建一个可容纳 6 个单词的新字典,复制旧单词并将新单词添加到新字典中。

在我的 main 函数中,我创建了一个 lexicon**(指向指针数组的指针,因为每个单词都有一个 char 指针到它)。 我创建了一个 newStr 函数来接收新单词并将其添加到字典中并按字母顺序对其进行排序。

程序运行一次,但当我想添加另一个词时,我收到访问冲突警告:

0xC0000005: Access violation reading location 0xDDDDDDDD.

我不明白我做错了什么。感谢您的帮助!

这是我的代码:

#define MAX 80
#include <iostream>
#include <cstring>
#include <string.h>
using namespace std;

void newStr(char** lexicon, int& lexiconSize, char word[])
{
    // create the new updated lexicon
    char** updated = new char*[++lexiconSize];

    // copy the words from the old to the updated lexicon
    for (int i = 0; i < lexiconSize; i++)
    {
        updated[i] = new char[MAX];

        if (i < lexiconSize - 1)
        {
            strcpy_s(updated[i], MAX, lexicon[i]);
        }

        // add the new word to the end of the updatedLexicon
        else
        {
            strcpy_s(updated[i], MAX, word);
        }
    }

    // deallocate the memory of the worlds of the old lexicon
    for (int i = 0; i < lexiconSize - 1; i++)
    {
        delete[] lexicon[i];
    }

    // deallocate the memory of the old lexicon
    delete[] lexicon;

    // point the lexicon pointer to the updatedLexicon
    lexicon = updated;

    // now sort the lexicon including the new word
    if (lexiconSize > 1)
    {
        for (int i = 1; i < lexiconSize; i++)
        {
            for (int j = 1; j < lexiconSize; j++)
            {
                if (strcmp(lexicon[j - 1], lexicon[j]) > 0)
                {
                    char t[MAX];
                    strcpy_s(t, MAX, lexicon[j - 1]);
                    strcpy_s(lexicon[j - 1], MAX, lexicon[j]);
                    strcpy_s(lexicon[j], MAX, t);
                }
            }
        }
    }

    // deallocate the memory created for the updated lexicon
    for (int i = 0; i < lexiconSize; i++)
    {
        delete[] updated[i];
    }

    delete[] updated;

    return;
}

int main()
{
    int lexiconSize = 3;
    char** lexicon;
    char word[MAX] = {};

    // initialize lexicon for testing:
    lexicon = new char*[lexiconSize];
    lexicon[0] = new char[MAX];
    strcpy_s(lexicon[0], MAX, "maybe");
    lexicon[1] = new char[MAX];
    strcpy_s(lexicon[1], MAX, "this");
    lexicon[2] = new char[MAX];
    strcpy_s(lexicon[2], MAX, "works");

    cout << "enter the word to add" << endl;
    cin >> word;

    newStr(lexicon, lexiconSize, word);

    // menu system that allows to add/delete/print the words

    // delete the lexicon at the end of the program
    for (int i = 0; i < lexiconSize; i++)
    { // delete the internal words
        if (lexicon[i])
        {
            delete[] lexicon[i];
        }
    }

    if (lexicon)
    {
        delete[] lexicon;
    }

    return 0;
}

最佳答案

您的问题是 lexicon 按值传递给 newStr()

赋值 lexicon = updated 因此对调用者不可见。

由于该函数释放了 lexicon[i] 引用的所有动态分配的内存,因此在 main() 中对 lexicon 的所有后续使用都有未定义的行为。

顺便说一下,在 newStr() 中分配的所有内存都被泄漏了——函数返回后没有变量引用它,所以它不能在代码中释放。

与其尝试直接使用指针和运算符 new,不如查找标准容器 (std::vector) 和 std::string (管理字符串数据)。

关于c++ - 动态扩展指针数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48248614/

相关文章:

c++ - std::initializer_list 作为重载运算符的右手参数?

c - 关于动态分配内存给 char 指针的简单问题

c - 如何在 C 中动态分配二维数组?

c - 在 C 中使用 malloc() 或 realloc() 存储并打印数据

c++ - C++ 多态性中的访问冲突(虚拟指针函数指向错误的位置?)

c++ - SDL_混音器 : crash Mix_FreeMusic();

c++ - 动态 vector 的预期内容

c++ - QByteArray::number(int) 只有两个字节长

c++ - 捕获dll异常后访问冲突

c++ - 佳能 SDK 初始化崩溃