简体   繁体   English

C++:将动态分配的数组打印到控制台,行为异常

[英]C++: Printing dynamically allocated array to console behaving weirdly

I am having weird behavior with my print function but I am wondering if it is an IDE issue or an issue with the fact that my code and target/executable is stored on an external hard drive.我的打印功能出现了奇怪的行为,但我想知道这是 IDE 问题还是我的代码和目标/可执行文件存储在外部硬盘驱动器上的问题。 OR if it is my code.或者,如果它是我的代码。 it will randomly print things like '/323' '2u3m' that kind of stuff.它会随机打印诸如 '/323' '2u3m' 之类的东西。 Then I run it again and it prints totally fine.然后我再次运行它,它打印得很好。

int WordList::print() const {
    
    if (m_list != nullptr) {
        for(int i=0; i<(m_count-1); i++){
            cout << m_list[i] << " ";
        }
    cout << m_list[m_count-1] << endl;
    return m_count;
    }
    else
    
    return -1;
}

Also the computer I am using has 4gb of RAM and is often kinda slow... I am using Xcode此外,我使用的计算机有 4gb 的 RAM,而且通常有点慢......我正在使用 Xcode

I realize you do not see the rest of my code and that their may be some kind of issue there.我意识到您没有看到我的其余代码,并且它们可能存在某种问题。

I haven't been coding very long so this kind of behavior maybe a sign of something but I haven't had the experience to know what it is so I am hoping someone here can help me out!我写代码的时间不长,所以这种行为可能是某种迹象,但我没有经验知道它是什么,所以我希望这里有人可以帮助我!

Thanks!谢谢!

Member variables:成员变量:

class WordList {
.
.
.
private:

    unsigned int m_count;   // Number of words currently in list
    unsigned int m_max;     // The total size of the list.
    char**       m_list;    // The list storing the words

};

Adding where I think a problem would lie if there is one:如果有问题,请添加我认为问题所在的位置:

int WordList::add(const char word[]) {
    if (strlen(word)==0) {
          return -2;
        }
    if (m_list == nullptr ){
        m_list = new char*[1];
        m_list[m_count] = new char (strlen(word));
        strcpy (m_list[m_count], word);
        m_count++;
        m_max ++;
        return -2;
        }
    if (m_count == 0 && m_list != nullptr ) {
        m_list[m_count] = new char (strlen(word));
        strcpy (m_list[m_count], word);
        m_count++;
        return 0;
        }
    if (m_count < m_max) {
        m_list[m_count] = new char (strlen(word));
        strcpy (m_list[m_count], word);
        m_count++;
        return 0;
        }
    if (m_count == m_max) {
        m_list[m_count] = new char (strlen(word));
        strcpy (m_list[m_count], word);
        m_count++;
        m_max ++;
        return 1;
        }
else
    return -2;
}

So I am using this to store char arrays, I have to only use things in the cstring library hence the reason I am doing everything the hard way.所以我用它来存储 char 数组,我必须只使用 cstring 库中的东西,因此我做所有事情的原因都是困难的。

This is a project for school if it isn't obvious and I know there are details I may have left out as to the greater scope of the project but I think it should make sense with the information I have given.这是一个学校项目,如果它不明显的话,我知道关于项目的更大范围,我可能遗漏了一些细节,但我认为我提供的信息应该是有意义的。

This line, and lines that are similar to this:此行以及与此类似的行:

m_list[m_count] = new char (strlen(word));

are wrong for two reasons.错误的原因有两个。

First, that line does not allocate strlen(word) chars dynamically.首先,该行不会动态分配strlen(word)字符。 What it does is it creates a single character dynamically, with the value of that character equal to strlen(word) .它的作用是动态创建单个字符,该字符的值等于strlen(word)

Second, even if you allocated correctly, it does not allocate enough memory to hold the terminating null character when you call strcpy later on.其次,即使您正确分配,当您稍后调用strcpy时,它也不会分配足够的内存来保存终止null字符。

Thus your program had memory overwrites and undefined behavior.因此你的程序有内存覆盖和未定义的行为。

The fix for this line (and other similar lines is):此行(和其他类似行)的修复是:

m_list[m_count] = new char [strlen(word) + 1];

Note the [ ] and the addition of the +1 to make room for the null terminator.注意[ ]+1的添加,以便为空终止符腾出空间。


Other issues that are suspicious is your usage of m_count and m_list[m_count] .其他可疑的问题是您对m_countm_list[m_count] Where do you actually increase the size of m_list to hold more items?您实际上在哪里增加m_list的大小以容纳更多项目?


Having said all this, why are you not simply using std::string , so that these types of mistakes are not committed?说了这么多,为什么不简单地使用std::string ,这样就不会犯这些类型的错误呢? Also, why not simply std::vector<std::string> m_list instead of raw pointers?另外,为什么不简单地使用std::vector<std::string> m_list而不是原始指针?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM