0

I know that if there is even no object in array there is still some address. But I want to find solution to check if there is an object under the specific index we will ask. I need to have such mechanism to adding new Points to polygon. But before that I need to know if counter of object should grow or stay in the same value if there was an object. Maybe I should try fill all array with NULL?

main.cpp

#include <iostream>
#include "punkt.h"
#include "wielokat.h"

using namespace std;

int main(int argc, char *argv[])
{
    Punkt  p1("p1", 10, 20); // 10x20
    Punkt  p2("p2", 1, 1);   //1x1

    Wielokat w1 ("square", 4);

    w1.set(p1,0);
    w1.set(p2,0);
    w1.showWielokat();


    system("PAUSE");
    return EXIT_SUCCESS;
}

Wielokat.cpp

#include "punkt.h"
#include "wielokat.h"
#include <iostream>

using namespace std;

void Wielokat::increase(int n)
{
    m_ilosc = m_ilosc + n;
    m_tab = new Punkt * [m_ilosc];  
    cout<<"Dodaj "<<m_ilosc<<endl;
}

void Wielokat::decrease(int n)
{
    m_ilosc = m_ilosc - n;
    if(m_ilosc<0){ m_ilosc=0;}
    m_tab = new Punkt * [m_ilosc]; 
    cout<<"Odejmij "<<m_ilosc<<endl;
}
void Wielokat::set(Punkt p, int pos)
{
    //How to check if there was already object ?
    m_tab[pos] = new Punkt(p);
    m_counter++;
}

void Wielokat::showWielokat()
{
    for(int i=0; i<m_counter; i++){
        m_tab[i]->show();
    }

}
void Wielokat::crash(int pos){
    //after delete all elements moved one by one to the left side
    delete m_tab[pos];
    for(int i=pos; i<m_ilosc; i++){
        m_tab[i]=m_tab[pos+1];
    }
}
double Wielokat::getParimeter(){
    //here is function whih will count circuit 
}

Wielokat.h

class Wielokat {

    public:

    Wielokat(char* nazwa, int ilosc):m_nazwa(nazwa), m_ilosc(ilosc) 
    {
        m_tab = new Punkt * [m_ilosc]; 
        m_counter = 0;
    }

    Wielokat(const Wielokat& p): m_ilosc(p.m_ilosc), m_nazwa(strdup(p.m_nazwa))
    {}

    ~Wielokat()
    {
        for(int i=0; i<m_counter; i++){
            delete m_tab[i];
        }
        delete m_tab;
    }

    //Function:
    void increase(int n);
    void decrease(int n);
    void set(Punkt p, int pos);
    void crash(int pos);  //delete
    void showWielokat();
    double getParimeter();



    private:
    Punkt **m_tab;  //our tab of elemenst
    char* m_nazwa;
    int m_ilosc;
    int m_counter;
};

3 Answers 3

6

You are coding in C++, which means you can have an std::vector<Punkt> (or an std::vector<Punkt*>, if polymorphism were required). Don't reinvent the wheel; use it.

With an std::vector all of the manual allocation code is simply not required and you can check how many elements there are with vec.size().

Update: OK, so you can't use vector because this is homework.

The alternative solution is to zero out the memory of your array whenever you initialize it and then check if m_tab[i] == 0 before trying to use object i. Using memset, that would look like

// WARNING! INCOMPLETE/BUGGY CODE!
m_tab = new Punkt* [m_ilosc];
memset(m_tab, 0, m_ilosc * sizeof(m_tab[0]));

And since you are doing this from two places in the class, you should move this logic inside a separate private method.

Regarding the incomplete/buggy part, those two lines above have a few problems:

  1. The "old" array (if one exists) is not delete[]d; this is a memory leak.
  2. The values from the "old" array (if one exists) should be copied to the new array; the way things are now they are simply lost.

You should fix these as the next step.

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

4 Comments

But in this case i can't use std:vector.
Yes, it is. I don't know maybe I should make all elements NULL or checking by sizeof if this will be smaller than object is inside?
One more question: There is any way to delete single element of array ?
@mathewM: To remove it from the array, move all elements after it one position earlier and then set the position where the previously last element was to 0.
0

If you needv to check for values in a collection, you'd be better using an associative container such as a map or a set from STL. I see no reason from your code that you can swap this in pretty easily.

Comments

0

Besides Jon's advice, C++ does not hold your hands, so you will need to explicitly maintain the valiud elements inthe array: either by a) using a max-valid-index or b) using NULL values for invalid entries (preferably both)

Note: using NULL would only work for storing pointers in the array. Otherwise you will need to have an invalid value (e.g a Punkt with too large/too small coordinates)

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.