Dynamic memory reallocation

This is a discussion on Dynamic memory reallocation within the C++ Programming forums, part of the General Programming Boards category; Hi. I know that this may be achieved by using STL or MFC, but I have a project for school ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    87

    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. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    the second version should work fine. The delete[] operator knows how many elements are in the array because the new[] operator told it so
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    87
    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;
    Last edited by Gravedigga; 05-15-2005 at 12:13 PM.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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?
    You're only born perfect.

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    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++.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    87
    Quote Originally Posted by elad
    I have a concern about internal consistency with your code.
    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.
    Last edited by Gravedigga; 05-15-2005 at 05:28 PM.

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Quote 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{
        void* addr; //adress of first byte
        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;
        return memory_available.addr;
    }
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copy .dat file to dynamic memory...
    By IndioDoido in forum C Programming
    Replies: 5
    Last Post: 05-28-2007, 05:36 PM
  2. dynamic memory allocation problem
    By firyace in forum C Programming
    Replies: 4
    Last Post: 05-23-2007, 10:57 PM
  3. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 02:41 PM
  4. dynamic memory + linked lists
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 02-10-2002, 04:50 PM
  5. Dynamic Memory Allocation for fstream (binary)
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2001, 10:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21