Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7950863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:23:43+00:00 2026-06-04T02:23:43+00:00

I recently started with C++ and i’m not entirely sure I grasp the concept

  • 0

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;

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T02:23:46+00:00Added an answer on June 4, 2026 at 2:23 am

    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++;
          }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently started with linq to sql and i am not sure what is the
I recently started programming C on Mac OS X (10.7.4). I have the various
I recently started using android actionbars and contextual action bars (CAB). I have just
I recently started a project where I have to authenticate users against the Active
I recently started to use linux, so I have little knowledge about it. At
We recently started providing a data extract to clients via an XML. We have
I recently started using WPF and the MVVM framework, one thing that I have
Recently started with OpenGL, I have managed to draw a background image on screen
I recently started a new webforms project and decided to separate the business classes
Recently started with ASP.NET and MVC and have a few questions on working with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.