简体   繁体   English

C ++,尝试打印整数时cout的奇怪行为

[英]C++, Weird behavior of cout when trying to print integers

Im trying to write a class that stores an id and a value in an container class. 我试图编写一个在容器类中存储id和值的类。 Im using an nested class as my data structure. 我使用嵌套类作为我的数据结构。 When im compiling the code sometimes it prints perfectly, sometimes it prints nothing and sometimes it prints half of the data then stops. 当我编译代码有时它打印完美,有时它什么都不打印,有时打印一半的数据然后停止。 When i debug the code the same weird behavior occours, when it fails during debug it throws an error "Map.exe has triggered a breakpoint.", the Error occours in the print method when im using cout. 当我调试代码时,同样奇怪的行为发生,当它在调试期间失败时抛出错误“Map.exe已触发断点。”,当我使用cout时,错误在print方法中出现。

cmap.h cmap.h

#pragma once


class CMap
{
public:
    CMap();
    ~CMap();
    CMap& Add(int id, int value);
    void print() const;


private:

    class container
    {
    public:
        ~container();
        int container_id = 0;
       int container_value = 0; 
    };
    container* p_komp_;
    int dim_ = -1;

    void resize();
};

cmap.cpp cmap.cpp

#include "cmap.h"
#include <iostream>
using namespace std;



CMap::CMap()
{
    p_komp_ = new container[0];
}


CMap::~CMap()
{
    p_komp_ = nullptr;
    cout << "destroy cmap";
}


CMap& CMap::Add(int id, int value)
{
    resize();
    p_komp_[dim_].container_id = id;
    p_komp_[dim_].container_value = value;
    return *this;
}

void CMap::resize()
{
    container* temp_array = new container[++dim_];

    if (dim_ == 0)
    {
        temp_array[0].container_id = p_komp_[0].container_id;
        temp_array[0].container_value = p_komp_[0].container_value;
    }
    for (unsigned i = 0; i < dim_; i++)
    {
        temp_array[i].container_id = p_komp_[i].container_id;
        temp_array[i].container_value = p_komp_[i].container_value;
    }

    p_komp_ = temp_array;

}

void CMap::print() const
{

    for (unsigned i = 0; i <= dim_; i++)
    {    

        cout << p_komp_[i].container_id;
        cout << p_komp_[i].container_value;

    }
}


CMap::container::~container()
{
    cout << "destruct container";

}

Map.cpp Map.cpp

#include "cmap.h"
#include <iostream>

using namespace std;

    void main(void)
    {

        CMap m2;
        m2.Add(1, 7);
        m2.Add(3, 5);
        m2.print();
    }

These two things are a possible reason for your problem: 这两件事可能是您遇到问题的原因:

int dim_ = -1;

and

container* temp_array = new container[++dim_];

When you allocate, you increase dim_ from -1 to 0 . 分配时,将dim_-1增加到0 That is you create a zero-sized "array", where every indexing into it will be out of bounds and lead to undefined behavior . 那就是你创建一个零大小的“数组”,其中每个索引都将超出界限并导致未定义的行为


You also have memory leaks since you never delete[] what you new[] . 你也有内存泄漏,因为你永远不会delete[]new[] I didn't look for more problems, but there probably a more. 我没有找到更多问题,但可能还有更多问题。

And an "array" (created at compile-time or through new[] ) will have indexes from 0 to size - 1 (inclusive). 并且“数组”(在编译时或通过new[] )将具有从0size - 1 (包括)的索引。 You seem to think that the "size" you provide is the top index. 您似乎认为您提供的“大小”是最高指数。 It's not, it's the number of elements. 它不是,它是元素的数量。

It seems to me that you might need to take a few steps back, get a couple of good books to read , and almost start over. 在我看来,你可能需要退一步, 拿几本好书来阅读 ,几乎重新开始。

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

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