简体   繁体   中英

Pointers and memory allocation in C++

I recently started with C++ and i'm not entirely sure I grasp the concept of pointers and their connection to arrays. I have two classes, Term and Polynom. I have a main loop which allows the user to enter 2 numbers. Those numbers is then added to the "Term" object and that object is then added to the "Polynom" object. Everytime the loop is executed a new "Term" object is created.

 //These lines are executed until the user is done entering numbers             
 potens = new Term;
 potens->sattPotens(kinput, ninput);//Add values to "Term object"
 poly.addTerm(potens);//Add "Term" object to "Polynom" object

A "Polynom" object is only created once in the program. In the "Polynom" class I use a "Term" pointer to store all the "Term" objects that is added to the "Polynom" object. The "Term" pointer in the "Polynom" class is initiated once in the "Polynom" constructor.

 void Polynom::addTerm(Term *t){
      *(term+antal_termer) = *t;//This is were the program crashes
      antal_termer++;
}

I know I could use a vector instead of a pointer to store the "Term" objects but i'm trying to learn how pointers work. I am also unsure when I'm supposed to delete the objects created in the main loop. Since every time the loop is executed I create a new "Term" object but I never delete them.

EDIT: I used to allocate the "Term" object in the "Polynom" class this way: term = new Term[]; I then changed it to term = new Term[10]; but I still crashes when I execute term[antal_termer] = *t;

 *(term+antal_termer) = *t;//This is were the program crashes
 antal_termer++;

This crashes because you probably haven't allocated enough memory. Your best choice is to use a std::vector instead of a dynamic array.

Is term allocated term = new Term; or term = new Term[sz]; ?

If it's the first, you can only store one object, and term+antal_termer goes beyond that. If it's the second, you run into problems if antal_termer >= sz .

The std::vector option gives you automatic management:

 std::vector<Term> terms;
 Term potens; //why use new?
 terms.push_back(potens);

Note that I'm using objects, not pointers. For pointers, it'd be

 std::vector<Term*> terms;
 Term* potens = new Term;
 terms.push_back(potens);

But note that you have to delete the memory when you're done with it.

Pasting in outcome from comments.

antal_termer was not initialised in the constructor, resulting in invalid memory access here:

*(term+antal_termer) = *t;

As the code is copying t , via assignment, you can delete potens; after the call to addTerm() . The code must prevent going beyond the end of the term array in addTerm() , otherwise another invalid memory access will occur:

void Polynom::addTerm(Term *t){
      if (antal_termer < 10)  // Use constant instead of literal 10
      {
          *(term+antal_termer) = *t;
          antal_termer++;
      }
}

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