简体   繁体   中英

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. 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.

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

#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

#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 . 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[] . 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). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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