0

I want to define dynamic array h of size size and later in other functions, modify and use it as here:

class definition:

    static int size=10;

class hash{
    public:
    string h[size];
    hash();
    void resize();
    void operations();
    void print();
};

hash::hash()
{
    h[size-1]="nikhil"; //size=10 now.
}

/*Defining `h` as `string* h=new string[size];` is not working. 
My compiler (MinGW on Windows 7) show error: dynamic allocation is not allowed by default*/



 // resizing the array
    void hash::resize( )
    {
            string temp[2*size];
            for(int i=0;i<=size;i=i+1)
                {
                    temp[i]=h[i];
                }
            size=2*size;
        h=temp;
    }

    void hash::print()
    {

        for(int i=0;i<size;i=i+1)
            {if(!h[i].empty())
                {cout<<"h["<<i<<"]="<<h[i]<<endl;} 
            }
    }

int main()
{
    hash p;
    p.resize();//now size should change to 20.
    p.print();
}

Where is the problem is it defining the size variable or in resizing the array?

Nikhil Chilwant
  • 629
  • 3
  • 11
  • 31

2 Answers2

4

Use std::vector if you need arrays of dynamic size.

class hash {
public:
    std::vector<std::string> h;
    hash();
    void resize();
    void operations();
    void print();
};

hash::hash() : h(10) {
    h[9] = "nikhil";
}

void hash::resize() {
    h.resize(2 * h.size());
}

Though note that std::vector does resizing for you automatically if you add new elements using push_back. Also note that the standard library has hash table data types already (std::unordered_set and std::unordered_map), so you don’t have to write them yourself.

  • actually this part is used in my hashing code. array h is my hash table and my hash function is dependent on array size. So, after resizing, it is not necessary that 1st element will always go to 1st place. So, I think vector is not good idea here. – Nikhil Chilwant Oct 27 '13 at 17:59
  • I’m not completely sure I understand you. Your original code exhibits UB by reading beyond the end of the array. How does your hash function depend on the size of the array? Besides, you can always reorder the elements of an `std::vector` to your heart’s desires. –  Oct 27 '13 at 18:02
  • word is string I am going to pass – Nikhil Chilwant Oct 27 '13 at 18:08
  • I still have no idea what you mean, sorry. –  Oct 28 '13 at 11:31
0

I do not know C++ but you haven't exactly told what is going on. But the way your resize() method is working is the for loop goes through 2*the size of H which will cause a problem.

When you loop through 2*size it is trying to loop through more items than what you actually have in the original array. you have to loop through the original array size.

for(int i = 0; i < h.size(); i++)
{
    temp[i] = h[i];
}

I can barely see the comments in your code they are too light for me so I didn't see them.

But to explain a little better i guess, lets say original array is size 5 your new one is size 10 when you loop through 10 items you dont have 10 items in the original array so you'll get errors when trying to access them.

MGHandy
  • 363
  • 1
  • 3
  • 11