2

パワーセットをファイルに書き込もうとしていますが、開始配列がサイズ 6 よりも大きいとヒープが破損し、その理由がわかりません。配列 6 以下の任意のサイズで問題なく動作します。これを理解することはできません。

また、test.txt は、配列を読み取った場所です。ファイルに「1,2,3,4,5,6」が含まれている場合は正常に動作しますが、「1,2,3,4,5,6,7」が含まれている場合、ヒープが破損します。

#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include "N26.h"
#include <math.h>

using namespace std;

void increaseArray(int* theArray, int size)  
{  
    int i = size;  
    int n = i+1;  
    int* newArray = new int[n];  

    for(int cnt=0;cnt<n;cnt++)
    {  
        newArray[cnt] = theArray[cnt];  
    }  

    newArray[n-1]= NULL;
    theArray = newArray;

    return;  
} 

void printPowerSet(int *s, int n)
{
    int i=0,j=0;
    ofstream myFile;
    double SetSize=pow(2.0,n);

    myFile.open("powerset1.txt", std::ios_base::app);

    cout<<"{size of  original}"<< n <<endl;
    cout<<"{number of sets}"<< SetSize-1 <<endl;

    for(i=1;i<SetSize;++i)
    {
        for(j=0;j<n;++j)
        {
            if(((i>>j)&1)==1)
            {           
                myFile << s[j] <<",";
            }
        }

        myFile<<endl;
    }

    return;
}

int main()
{
   ifstream myFile;
   int item;
   string input ="";

   string fileName = "test.txt";

    myFile.open(fileName);
    while(myFile)
    {   
        int k = 1;
        int* transaction= new int[1];

        if(!getline(myFile,input))
            break;

        istringstream ss(input);
        while(ss)
        {
            if(!getline(ss,input, ','))
                break;

            input.erase(remove_if(input.begin(), input.end(), isspace), input.end());
            item = atoi(input.c_str());
            transaction[k-1] = item;
            increaseArray(transaction,k);

            k++;
        }

        for(int i =0; i<k-1;i++)
        {
            cout << transaction[i];
        }
        printPowerSet(transaction, k-1);
            cout << endl;
        transaction=NULL;
}

    system("Pause");
   return 0;
}
4

2 に答える 2

2

increaseArray()ポインターのローカル コピーのみを変更しているため、関数は機能しません。必要なことを行うには、二重ポインターまたはポインター参照を渡す必要があります。

ポインターへの参照の例: void increaseArray(int*& theArray, int size)

代わりに、std::vectorこれは自動的に大きくなるため、を使用することをお勧めします。

これがあなたの問題に関係しているとは思えませんが、削除したこともありません。あなたはメモリをリークしています。新しい割り当てでポインターを再割り当てする前に、古い割り当てを削除します。

delete [] theArray; // The "[]" is important!
theArray = newArray;
于 2012-12-14T19:05:49.777 に答える
1

フレッドの答えに加えて。

内部で何が起こっているかincreaseArray()、特に次の行を見てください。

int i = size;  
int n = i+1;  
int* newArray = new int[n];  

for(int cnt=0;cnt<n;cnt++)
{  
    newArray[cnt] = theArray[cnt];  
}  

size + 1要素の配列を割り当ててから、元の配列を反復処理します。つまり、元の配列の外側にある 1 つの要素にアクセスしています。ヒープのレイアウト方法によっては、セグメンテーション違反が発生する可能性がありますが、未定義の動作であることは確かです。new

于 2012-12-14T19:09:33.050 に答える