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 5985219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:29:53+00:00 2026-05-22T22:29:53+00:00

i do this in the education purpose. such is idea: i have class Array.

  • 0

i do this in the education purpose.
such is idea:
i have class Array. I inheritance it in class Darray, and add necessary dimension

it likes so:

main.cpp

int main(int argc, char *argv[])
{


  Array obj;
  obj.output();

 DArray obj2;
 obj2.output();

   return 0;

}

header

      class Array

       {
        public:
          int  *x,sizem;  //одно измерение, массив
                // можно объявить здесь как  **x , но это не то

      Array();
       ~Array();
   virtual  void output();
   };


   class DArray:public Array
 {
public:
 int **z;
  int sizen;
   DArray();
~DArray();
 virtual void output();
};

realisation.cpp

           Array::Array()
        {
        sizem=5;

        x=new int[sizem];

         for(int i=0;i<sizem;i++)
       x[i]=i;
         }

        Array::~Array()
     {
        delete []x;
       }


       void Array::output()
       {
       for(int i=0;i<sizem;cout<<x[i]<<" ",i++);
       cout<<"\n";
        }


           void DArray::output()
      { cout<<"\n";

       for(int i=0;i<sizem;i++)
         {   for(int j=0;j<sizen;j++)
               cout<<z[i][j]<<" ";
             cout<<"\n";
              }
              }       


        DArray::DArray()
           {
            int i;
           sizen=sizem;

           z=new int*[sizem];   // одно измерение уже есть, т.е. это одна строка


             for(i=1;i<sizem;i++)
               z[i]=new int[sizem];


            for(int i=1;i<sizem;i++)   // if i don't initialization  matrix, all'll 

          works right,compiles,and run, but AFAIK  when i allocate space dynamically,
         it must be intitializationed self by zeros, but it isn't particually equal
          zeroes, the fisrt two  column are equal  some random number, other 
       are equal zero.


             for(int j=0;j<sizen;j++)
               {
                z[i][j]=2;
                }

              *z=x;  // i've already array x , that was inheritanced, just point address
            }



                DArray::~DArray()
           {


          for(int i=1;i<sizem;i++)
           delete z[i];


           // yet one problem 
           // i can't delete  x  e.g. delete []x
         // but it should work! 

                 delete []z;
                   }

PS I works without errors, but i think there are memory leak, but can’t catch up why

all tricks has been done in constructor, everything other is general stuff like output matrix/array. that’s why i think that problem is there

thanks you  mates!

but i don’t understand yet one thing

          z=new int*[sizem];  

           for(i=1;i<sizem;i++)
          z[i]=new int[sizem];


       for(int i=1;i<sizem;i++)
      {
         for(int j=0;j<sizen;j++)

           cout<<z[i][j]<< " ";
           cout<<"\n";
       }
 as i think i have to receive here  ones zeros. byt i receive something like

          2532336 2532336 0 0 0 
          2532336 2532336 0 0 0 
          2532336 2532336 0 0 0 
          2532336 2532336 0 0 0 

Why?

  • 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-05-22T22:29:53+00:00Added an answer on May 22, 2026 at 10:29 pm

    There are no memory leaks, but there is one mismatch between new and delete:

    You’re using new[] here:

    for(i=1;i<sizem;i++)
        z[i]=new int[sizem];
    

    but you’re using delete (not delete[]) here:

    for(int i=1;i<sizem;i++)
        delete z[i];
    

    With that changed to delete[] z[i];, there are no warnings from valgrind:

    ==29235== HEAP SUMMARY:
    ==29235==     in use at exit: 0 bytes in 0 blocks
    ==29235==   total heap usage: 7 allocs, 7 frees, 160 bytes allocated
    ==29235== 
    ==29235== All heap blocks were freed -- no leaks are possible
    

    EDIT: regarding your second question, if you want the arrays to be initialized with zeroes, use

    z[i] = new int[sizem](); // note the pair of parentheses
    

    or just use vectors like everybody else.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is baffling me, maybe somebody can shine the light of education on my
I am asking this question from an educational/hacking point of view, (I wouldn't really
How can i convert this to a decimal in SQL? Below is the equation
This is a bit of a long shot, but if anyone can figure it
This is starting to vex me. I recently decided to clear out my FTP,
This is kinda oddball, but I was poking around with the GNU assembler today
This might seem like a stupid question I admit. But I'm in a small
This is a difficult and open-ended question I know, but I thought I'd throw
This is my first post here and I wanted to get some input from
This past summer I was developing a basic ASP.NET/SQL Server CRUD app, and unit

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.