为什么我在动态内存中丢失了数据?

时间:2017-01-15 20:21:31

标签: c++ pointers memory-leaks dynamic-memory-allocation

我目前正在使用C ++进行实现。我是C ++的新手,想要变得更好。在任务的最初步骤中,我需要读取一个文件并将一部分数据存储在一个类型为类的动态数组中," Sequence",如下所示;

class Sequence{
private:
    char *seq;
    int length;
public:
    /* Setter and getter declarations */
    void setSeq(char *);
    char *getSeq();
    void setLength(int);
    int getLength();

    /* Constructors and destructor */
    Sequence();
    Sequence(std::string);
    Sequence(std::string, unsigned);
    ~Sequence();

    /* Overloaded = operator */
    void operator = (Sequence *sequence)
    {
        seq = sequence->seq;
        length = sequence->length;
    };
 };

/* Constructor definitions */
Sequence::Sequence(){/* Default Constructor*/}
Sequence::Sequence(std::string str)
{
    seq = &str[0u];
    length = str.length();
}
Sequence::Sequence(std::string str, unsigned count)
{
    seq = &str[0u];
    length = count;
}

/* Destructor definition */
Sequence::~Sequence(){/* Default Destructor */}

/* Setter and getter definitions */
void Sequence::setSeq(char *seq){
    this->seq = seq;}
char * Sequence::getSeq(){
    return seq; }
void Sequence::setLength(int length){
    this->length = length; }
int Sequence::getLength(){
    return length; }

/* Forward declaration of file reading functions */
unsigned getCountOfSequences(char *);
Sequence *getSequences(char *, unsigned);

以下是cpp文件;

#include <fstream>
#include "file_operations.h"


unsigned getCountOfSequences(char *fileName)
{
    std::ifstream file(fileName);
    std::string str;
    unsigned count;

    /* Get total number of sequences*/
    file.unsetf(std::ios_base::skipws);
    count = std::count(
                       std::istream_iterator<char>(file),
                       std::istream_iterator<char>(), '\n')/4;
    file.close();

    return count;
}

/*
 *  Function : read_sequences
 *  Goal     : reading the file in FASTQ format to
 *  get all the sequences into a set in Sequence type
 *  Input    : name of the file
 *  Output   : a set of sequences in Sequence type
 */
Sequence *getSequences(char *fileName, unsigned count)
{

    /* Used variables while reading file */
    std::ifstream file(fileName);
    std::string str;

    /*Define a set of sequence dynamically*/
    Sequence *setOfSeqs = new Sequence[count];

    /* Put total number of seqs into first seq */
    setOfSeqs[0] = new Sequence("COUNT", count);


    /* Read each sequence and put it into set */
    int lineIndex = 1; int seqIndex = 0;
    while (getline(file, str))
    {
        /* This is the line of sequence */
        if ( lineIndex == 2 )
        {
            /* Define a temporary sequence */
            Sequence *newSeq = new Sequence(str);

            /* Add new seq to set of sequences */
            setOfSeqs[++seqIndex] = new Sequence(str);

            /* --- PART A --- */
            //delete newSeq;
            std::cout << setOfSeqs[seqIndex].getSeq() << "\n";

        }
        /* Loop updates */
        lineIndex = (lineIndex == 4) ? 0 : lineIndex;
        lineIndex ++;
    }
    file.close();

    /* --- PART B --- */
    for ( int i = seqIndex; i > 0; i -- )
    {
        std::cout << setOfSeqs[i].getSeq() << "\n";
    }*/
    return setOfSeqs;
} // read_sequences() function

现在,我有两个主要问题需要理解C ++中的指针和动态内存分配。你得到了,我只想读取文件,将数据放入一个数组(动态定义在&#34; Sequence&#34;类型中)并将其返回到另一个要使用的.cpp文件中。

我的第一个问题是关于循环结束后数据的变化。有两个部分被注释为&#34; PART A&#34;和&#34; B部分&#34;专注在。我在&#34; PART A&#34;中读到的所有数据。正确地逐行打印出来。但是,如果我尝试在&#34; PART B&#34;中打印这些行(存储在setOfSeqs中),我会遇到不同的数据,这是不正确的,尤其是大多数相同的数据。现在,为什么我丢失了我在while循环中读取的数据(或数据不同)?

我的第二个问题是关于在C ++中重新分配内存。如你所见,在while循环中,我创建了一个&#34; Sequence&#34;对象并在构造之后将其添加到数组setOfSeqs中。现在,如果我取消注释该行,删除newSeq;,在&#34; PART B&#34;中,在我将对象(newSeq)复制到其中一个setOfSeqs元素后,为什么我在尝试打印setOfSeqs时得到一个空字符串元素在下一行?实际上,我刚刚删除了新创建的对象,而不是setOfSeqs元素。从我的角度来看,我将对象均衡为元素,因此不应丢失setOfSeqs元素中的数据。

此外,即使我能够在&#34; B部分&#34;中打印出正确的数据。 (在返回setOfSeqs之前)一次,另一个.cpp文件中print语句的结果再次为空?

提前谢谢大家。

0 个答案:

没有答案