Memory Allocation/De-allocation
I''m sure I''m the only person here who doesn''t know the answer to these questions, but I''m going to swallow my pride and ask them anyway. At least I''m bound to get a lot of helpful replies.
This is basically a broad question about the fundamentals of declaring variables/objects/pointers/whatever and allocating/de-allocating memory.
Question 1) In which cases would you use the new/delete operators over malloc()/free() (if that''s even a correct comparison)?
Do they simply allocate memory from different places, or is there more to it than that?
Question 2) What''s the difference between:
a) MyObject *ptr1 = &MyObject();
b) MyObject *ptr2 = new MyObject();
Are those even comparable/correct statements? Is there a third variation using malloc()?
Question 3) Do these two code fragments accomplish the same thing? If so, why use one over the other? Again, is there a third variation using malloc()?
a) int *ptr1 = new int[10];
b) int *ptr2, i[10];
ptr2 = &i
I think I''ll just leave it at that. These questions aren''t so much about proper syntax as the reason behind using different methods that accomplish the same thing (if they do accomplish the same thing). I''ve searched back pretty far in the message boards to find answers, but couldn''t come up with anything. The documentation in the MSDN Library just didn''t seem to make it clear either.
I''m embarassed to post such ridiculously simple questions here, but this is one of those things that manages to elude me every time I think I understand it.
Thanks.
October 15, 2000 09:16 PM
But i''m pulling this from memory and not actually looking at any references so I''ll probably be wrong in a few cases.
Answer 1) I believe new/delete are C++ specific so if your coding for C only you don''t want to use them. It''s a little easier to override new/delete in C++ but you can accomplish the same thing with some creative macro''s for malloc()/free().
Answer 2) a) will return a pointer to the constructor not a pointer to a new instance of the object. b) allocates memory for the new object.
Answer 3) a) allocates the array of int''s on the heap, b) puts the ints on the stack and puts the address of it into ptr2.
Answer 1) I believe new/delete are C++ specific so if your coding for C only you don''t want to use them. It''s a little easier to override new/delete in C++ but you can accomplish the same thing with some creative macro''s for malloc()/free().
Answer 2) a) will return a pointer to the constructor not a pointer to a new instance of the object. b) allocates memory for the new object.
Answer 3) a) allocates the array of int''s on the heap, b) puts the ints on the stack and puts the address of it into ptr2.
Q1. new/delete operators are more effecient and easier to use.
Q2. remember, in order to use the delete operator, you have to use the new operator. you can not use the delete operator to a pointer not initialized by the new operator. the first declaration will not work because you can''t assign an address of a data type. i''m assuming myobject is a custom data type right. you have to declare a variable of type myobject before you assign its address to a pointer
example: myobject sample();
prt1 = &sample
the second statement is valid.
Q3. the last statement should be like this "ptr2 = i;" remember that i is really a pointer to an array, hence i contains the address of i[0] not the value. you should use the first statement more often because when your building a fairly complex code, the length of an array is usually unknown and its easier to free the memory taken up by pointers (new/delete).
don''t be embarassed to ask this questions. do you think the people that will be answering your questions were born programmers?
Q2. remember, in order to use the delete operator, you have to use the new operator. you can not use the delete operator to a pointer not initialized by the new operator. the first declaration will not work because you can''t assign an address of a data type. i''m assuming myobject is a custom data type right. you have to declare a variable of type myobject before you assign its address to a pointer
example: myobject sample();
prt1 = &sample
the second statement is valid.
Q3. the last statement should be like this "ptr2 = i;" remember that i is really a pointer to an array, hence i contains the address of i[0] not the value. you should use the first statement more often because when your building a fairly complex code, the length of an array is usually unknown and its easier to free the memory taken up by pointers (new/delete).
don''t be embarassed to ask this questions. do you think the people that will be answering your questions were born programmers?
Thanks so much for your help. It''s a little clearer now, and I''m sure I''ll get better at it as I write more programs.
1) If you are programming in C++ always prefer the use of new/delete over malloc/free. The first reason is that new/delete is simpler to use. The second reason is that malloc/free doesn''t know about constructors and destructors. As an example (this is from Meyers book), suppose you have a Cat class
class Cat {
public:
// cat constructor & destructor
Cat(){itsAge=5; itsWeight=3;}
~Cat(){delete itsWeight;}
// data member variables
int itsAge;
int * itsWeight = new int;
};
and you wanted to allocate memory for an array of 10 Cat objects:
Cat * catArray1 = static_cast(malloc(10*sizeof(Cat))); // using malloc
Cat * catArray2 = new Cat[10]; // using new
Here, catArray1 points to enough memory to store 10 Cat objects, but it doesn''t really contain the objects. Futhermore, there is no easy way to initialize the Cats in catArray1. For instance, if we want to set the age of the 5th Cat in the catArray1 we have to do some arithmetic.
On the other hand, catArray2 contains 10 fully initialized Cats. The cat constructor was called automatically by the new operator when they were created. Changing the 5th cat''s age is as simple as catArray[5].itsAge = newAge.
Later on, when we want to free the memory used by the catArrays we would use
free(catArray1[]); // using free
delete [] catArray2; // using new
The use of free() creates a memory leak because it doesn''t call the Cat destructor to free the memory allocated for the member variable itsWeight.
Another reason to choose new/delete over malloc/free, is that you can overload the operator new if you want to, making out-of-memory conditions easier to handle. There is only about a 0.5% difference in speed allocating memory with new vs free, so I always choose new.
2. ptr1 points to an object on the stack and ptr2 points to an object on the heap. This is the only difference.
Case 2a) would be considered pretty obscure notation - I had to run a few test cases through my compiler to be sure what actually was going on. Case 2a) is equivalent to:
MyObject theObject(); // create an instance of the MyObject class using the MyObject() constuctor
MyObject * ptr1 = &theObject
One way to do the same thing with malloc() is
MyObject * ptr1 = static_cast(malloc(sizeof(MyObject)));
and then manually initializing the data members that your constructor MyObject() would initialize.
3) Read the replies by the first two posters. Keep in mind that
int * ptr1, ptr2; // don''t ever declare things this way. BAD!!
is really the same as
int * ptr1; // a pointer
int ptr2; // not a pointer!
class Cat {
public:
// cat constructor & destructor
Cat(){itsAge=5; itsWeight=3;}
~Cat(){delete itsWeight;}
// data member variables
int itsAge;
int * itsWeight = new int;
};
and you wanted to allocate memory for an array of 10 Cat objects:
Cat * catArray1 = static_cast(malloc(10*sizeof(Cat))); // using malloc
Cat * catArray2 = new Cat[10]; // using new
Here, catArray1 points to enough memory to store 10 Cat objects, but it doesn''t really contain the objects. Futhermore, there is no easy way to initialize the Cats in catArray1. For instance, if we want to set the age of the 5th Cat in the catArray1 we have to do some arithmetic.
On the other hand, catArray2 contains 10 fully initialized Cats. The cat constructor was called automatically by the new operator when they were created. Changing the 5th cat''s age is as simple as catArray[5].itsAge = newAge.
Later on, when we want to free the memory used by the catArrays we would use
free(catArray1[]); // using free
delete [] catArray2; // using new
The use of free() creates a memory leak because it doesn''t call the Cat destructor to free the memory allocated for the member variable itsWeight.
Another reason to choose new/delete over malloc/free, is that you can overload the operator new if you want to, making out-of-memory conditions easier to handle. There is only about a 0.5% difference in speed allocating memory with new vs free, so I always choose new.
2. ptr1 points to an object on the stack and ptr2 points to an object on the heap. This is the only difference.
Case 2a) would be considered pretty obscure notation - I had to run a few test cases through my compiler to be sure what actually was going on. Case 2a) is equivalent to:
MyObject theObject(); // create an instance of the MyObject class using the MyObject() constuctor
MyObject * ptr1 = &theObject
One way to do the same thing with malloc() is
MyObject * ptr1 = static_cast(malloc(sizeof(MyObject)));
and then manually initializing the data members that your constructor MyObject() would initialize.
3) Read the replies by the first two posters. Keep in mind that
int * ptr1, ptr2; // don''t ever declare things this way. BAD!!
is really the same as
int * ptr1; // a pointer
int ptr2; // not a pointer!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement