1. ## Dynamic memory reallocation

Hi.

I know that this may be achieved by using STL or MFC, but I have a project for school that must be written without these libraries.

I must create a simple program that deals with graphic objects- points, shapes and 3d figures. A shape object could be deffined by any number of points. So I have a pointer of type
Code:
`Point **Elements;`
My constructor gets three arguments:
Code:
```Shape::Shape(Point& p1, Point& p2, char* _Name)
{
num=2;
Elements = new Point*[2];
Elements[0]=&p1;
Elements[1]=&p2;
Name = new char[strlen(_Name)+1];
strcpy(Name,_Name);
}```

Now I have a method which must add a point to the elements of the shape:
Code:
```void Shape::AddPoint(Point* p)
{
Point **tmp_list;
tmp_list = new Point*[++num];
int i;
for(i=0;i<num-1;i++)
tmp_list[i]=Elements[i];
delete[] Elements;
Elements = new Point*[num];
for(i=0;i<num-1;i++)
Elements[i]=tmp_list[i];
Elements[num-1] = new Point(*p);
}```
My question is: Is this the proper way of adding a new point pointer to the elements or I just can do this:
Code:
```void Shape::AddPoint(Point* p)
{
Point **tmp_list;
tmp_list = new Point*[++num];
int i;
for(i=0;i<num-1;i++)
tmp_list[i]=Elements[i];
tmp_list[num-1]=p;
delete[] Elements;
Elements=tmp_list;
}[/```
And one more: How do the "delete[] " operator knows how many elements are there in the array?

2. the second version should work fine. The delete[] operator knows how many elements are in the array because the new[] operator told it so

3. Thank you.

But if it is so, If I do so:

Code:
`Elements=tmp_list;`
And if delete the array Elements, from where the delete[] operator could know the size of tmp_list array so to delete more memory?

the operator :
Code:
`tmp_list = new *Point[++num];`
Allocates more memory than the operator which has allocated memory for the Elements array;

4. I have a concern about internal consistency with your code. In the constructor you pass two Point objects by reference using reference variables as paramenters and a C style string by reference using a pointer to type char. Within the body of the constructor you appear to do a deep copy for the Cstyle string but not for the Point parameters, though all of the parameters are essentially pointers. If you wanted to gaurantee the integrity of the string within the constructed object by doing a deep copy, why not the Point parameters as well?

Then in AddPoint you pass a Point parameter by reference using a pointer to type Point. Why pass references to type Point to the constructor but pointer to type Point in AddPoint? And why not gaurantee the integrity of new Point being passed in to the Shape object by doing a hard copy of the Point pointer parameter?

5. whenever you get a pointer from new[] you actually get a pointer into a block of memory. that memory consists of two things. theres your half that you get a pointer to and theres the compilers half that has details in it so delete[] will know how many items to delete and how many destructors to call. So when u assign to elements the pointer temp_list you are also telling delete at the same time that the bookkeeping has changed. when delete[] is called on your pointer the compiler will look before your pointer typically at the record left by the new[] operator and act accordingly. As long as you remember to match new[] with delete[] on ALL types then you will have no problems. let the compiler do the bookwork and you need not worry about it. For a fuller explanation see Scott Meyers More Effective C++.

I wrote the above code just to ask my question as an example. In fact I changed it in my project after this.

And thanks to Stoned_Coder and elad. Now I'm sure what am I doing.

7. Originally Posted by Gravedigga
And one more: How do the "delete[] " operator knows how many elements are there in the array?
Have you ever wondered where the memory comes from?? Why don't pointers and memory regions overlap??
whenever you allocate memory, the info on the allocated region is stored wherelse, with region start and end, which includes size. So when calling delete, the operators acesses that info and knows how many memory is there.

Look at a small example
Code:
```struc block{
unsigned int sz;//size of region allocated from the first byte
} memory_available[100];

void *get_me_memory(unsigned int size){
int free_spot_index;
//iterate in memory_available and find a clear spot
memory_available[free_spot_index].sz = size;
}
bool dump_it(void *p){
int i;
for(i=0;i<100;i++){
if(memory_available[i] == p)
//do object cleaning, blablabla, whatever
//the size is stored in memory_available.sz
return true;
}
return false;
}```
The user only need to manipulate 2 functions. Any adicional info is stored way from the programmers knowledge, or acess.