Heap Corruption detected: after Normal block(#176)

时间:2017-01-10 02:26:26

标签: c++

So I got this introduction to Programming assignment, I have to write a program that find the nth member of the following sequence 1, 121, 1213121, 121312141213121.. and so on. Basically, the first member is 1, and every next one is made of [the previous member] [n] [the previous member]. N < 10. So I got this problem that I do not understand, tried searching for it in the internet but didn't get anything that can help me.

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

using namespace std;

int size(int n, int realsize);
int main()
{
    int n;
    cin >> n;
    if (n == 1) {
        cout << "1";
        return 0;
    }
    int helper = 0;
    char c = '2';
    char* look;
    char* say;
    say = new char[size(n, 1) + 1]();
    look = new char[size(n - 1, 1) + 1]();
    look[0] = '1';
    while (helper < n) {
        for (int i = 0; i < size(helper + 1, 1); i++) {
            say[i] = look[i];
        }
        say[size(helper + 1, 1)] = c;
        for (int i = size(helper + 1, 1) + 1; i < size(helper + 1, 1) * 2 + 1; i++) {
            say[i] = look[i - (size(helper + 1, 1) + 1)];
        }
        for (int i = 0; i < size(helper + 1, 1) * 2 + 1; i++) {
            look[i] = say[i];
        }
        helper += 1;
    }
    cout << say;
    delete[] say;
    delete[] look;
    return 0;
}

int size(int n, int realsize)
{
    if (n == 1)
        return realsize;
    else
        return size(n - 1, realsize * 2 + 1);
}

1 个答案:

答案 0 :(得分:0)

您正在覆盖look变量的容量。最后写的是say的全部内容,因此它也需要具有相同的大小。

虽然我不会宽恕下面的代码作为好代码,但它对您自己的实现进行的调整很少,并且应该为继续实现工作结果提供更坚实的基础。我用前几个数字测试了它,但这并不能保证它是完美的。

#include <iostream>

using namespace std;

int size(int n, int realsize);

int main()
{
    int n;
    cin >> n;
    if (n == 1)
    {
        cout << "1";
        return 0;
    }
    int helper = 0;
    char c = '2';
    char * look;
    char * say;
    say = new char[size(n, 1) + 1];     // Ditch the () call, which is confusing.
    look = new char[size(n, 1) + 1];    // Make the same size as "say"
    look[0] = '1';
    while (helper < n - 1)  // You're overrunning this loop I think, so I did it to n - 1.
    {
        for (int i = 0; i < size(helper + 1, 1); i++)
        {
            say[i] = look[i];
        }
        say[size(helper + 1, 1)] = c + helper;  // You were adding '2' every time, so this will add 2, 3, 4, etc incrementally.
        for (int i = size(helper + 1, 1) + 1; i < size(helper + 1, 1) * 2 + 1; i++)
        {
            say[i] = look[i - (size(helper + 1, 1) + 1)];
        }
        for (int i = 0; i < size(helper + 1, 1) * 2 + 1; i++)
        {
            look[i] = say[i];
        }
        helper += 1;
    }
    say[size(n, 1)] = '\0'; // Null-terminate "say" before printing it out.
    cout << say;
    delete[] say;
    delete[] look;
    return 0;
}

int size(int n, int realsize)
{
    if (n == 1)
        return realsize;
    else
        return size(n - 1, realsize * 2 + 1);
}