user2654735
user2654735

Reputation: 323

C++ windows has triggered a breakpoint - heap corruption

This error keeps being called and I can't figure out why.

enter image description here

Every time setNext( new Node(nullptr) ); is called this error pops up

full code:

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

class Node
{
    Node *next;
    int number;

    public:
        Node(Node *newNextNode) : next(newNextNode) { number = 0; };
        ~Node()
        {
            delete next;
        }

        int getNumber();
        void setNext(Node *newNextNode) { next = newNextNode; } ///This fails when called calling addNode()
        Node *getNext() { return next; }

};

int Node::getNumber()
{
    return number;
}


class LinkedList
{
    Node *list;

    public:
        LinkedList()
        {
            list = new Node(nullptr);
        }
        ~LinkedList()
        {
            delete list;
        }

        void addNode();
        void printList();
};

void LinkedList::addNode()
{
    Node *temp = list;

    while(temp->getNext() != nullptr)
    {
        temp = temp->getNext();
    }

    temp->setNext( new Node(nullptr) );

}

void LinkedList::printList()
{
    char *str = new char[sizeof(list->getNumber())];
    itoa(list->getNumber(), str, 10);

    Node *temp = list;

    while( temp->getNext() != nullptr )
    {
        char *c = new char[sizeof(temp->getNumber())];

        itoa( temp->getNumber(), c, 10 );

        char *tmp = new char[strlen(str) + strlen(c)];
        strcat(tmp, ' ' + c);
        strcat(tmp, str);

        str = new char[strlen(tmp)];
        strcpy(str, tmp);

        delete [] tmp;
        delete [] c;
    }

    string result(str);

    delete [] str;

    //return result;
}

int main()
{

    LinkedList *list = new LinkedList();

    list->addNode();

    list->printList();


    cin.get();

    delete list;
    return 0;
}

:) ok here is the full code. I know a lot of it is not the most efficient way of doing things. Ex. i'm doing printlist just to practice different things for a test.

Thanks for the help!

Upvotes: 0

Views: 568

Answers (2)

barak manos
barak manos

Reputation: 30136

Change this:

char* str = new char[sizeof(list->getNumber())]

The expression sizeof(list->getNumber()) is evaluated during compile-time as sizeof(int).

In fact, even if it was somehow "magically" evaluated during run-time, it would still not give you the actual string-length required.

What you need is to count how many digits (including the sign if necessary) are in number, and then allocate the required amount plus 1 (for the null-character).

By the way, since you're doing it in C++, you might as well just use a string.


UPDATE:

I just noticed that you've got a bunch of other string-related problems:

  • In the allocation of char* tmp, you should add 1 for the null-character
  • You have a problem similar to the one I've mentioned, with the allocation of char* c
  • You are reallocating str without deallocating it first (i.e., that will yield a memory leak)
  • None of your strings is null-terminated; applying strlen or strcat on any of them will therefore yield undefined behavior (and most likely, a memory access violation at some point)

Upvotes: 3

selbie
selbie

Reputation: 104494

This line looks suspicious:

char *str = new char[sizeof(list->getNumber())];

Assuming an sizeof(int) is 4 bytes, that means you are allocating a string that can only hold 3 characters plus an ending null char.

Typically, when I need to quickly allocate space to hold an integer converted to string, I'll multiple sizeof() * 5. That should be large enough to hold any base 10 string for any value of any integer type including negative values. Hence this is your new line:

char *str = new char[5*sizeof(list->getNumber())];

Upvotes: 0

Related Questions