1

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;

3
  • Can you show the Polynom constructor and definition of term. Commented May 18, 2012 at 10:02
  • Is antal_termer initialised in the constructor? You should also prevent storing more than 10 Term objects in addTerm(). Commented May 18, 2012 at 10:15
  • I added antal_termer = 0 in the constructor and it worked, thanks. I thought all the variables were initiated to 0 automatically when an object is created? Commented May 18, 2012 at 10:17

2 Answers 2

1
 *(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.

Sign up to request clarification or add additional context in comments.

4 Comments

I use term = new Term; But as I haver understood it if i do *(term+1) = *t the value of *t is simply stored in the memory cell next to the one allocated by term? It worked a few times when I did it that way.
@user1163392 how did you allocate term? Does the next memory cell actually exist? Are you allocating enough memory?
Hm, if I do term = new Term[10]; in the "Polynom" constructor the program still crashes the first time term[antal_termer] = *t; is executed. antal_termer=0 the first time.
Ok, reduce your sample and post a minimal one that reproduces the problem in ideone.com
0

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++;
      }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.