let say,
i have a House object;
House* someHouses = new House[100];
-----fucntion call... may or may not add stuff to someHouses ------

now, i want to get to the last position that haven't used yet. i tried to check NULL, and 0, both doesn't work. how can i check that?
between i can't use anything, for example vector, in STL library.

thanks in advance!

Recommended Answers

All 5 Replies

Keep a counter of how many houses you placed in the array.

Alternatively, you can use an array of pointers to House. IE:

int numHouses = 100;
House** someHouses = new House*[numHouses];
for (int i = 0; i < numHouses; i++)
{
    someHouses[i] = 0;
}

Now, when you want to see if you have a house there, the contents of that entry in the array will be 0 (null) if it hasn't been used yet.

cool, i never think the ways you two mentioned above, but i think i will use a counter to make it simple, i also have one more question about dynamic array
i am trying to make my array grow(2x of original and copy the old element to the new array) as it's full, and this is the way i tried to implement, but the compiler keep giving me segmentation fault. kinda annoying, i cant tell what i did wrong

numOfElement = number occupied the array, and arrSize is the total size of the array.

                        House* newHouses;
            if(numOfElement >= arrSize){

                int newSize = arrSize*2;
                newHouses = new houes[newSize];
                for(int i=0; i<arrSize; i++){
                    newHoues[i] = someHouses[i];
                }
                House* temp = records;
                someHouses = newHouses;
                delete [] temp;         
            }

1. Put your code inside code blocks (see formatting symbols on top of message input window).
2. Show ALL of your code.

You know if you use std::vector, it does that for you.

int main(){
 std::vector<int> array; //size = 0;
array.push_back( 1) ; //size = 1, [1]
array.push_back(5); //size = 2, [1,5]
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.