![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Sep 2005
Posts: 33
Rep Power: 0
![]() |
memory allocation and deallocation
suppose we have allocated dynamic memory by using new operator...can we use free to deallocte this memory..
working out with a example i think it works..but they say its not a good practice to do this,can anyone tell why? main()
{
int *k= new int;
int *j = (int*)malloc(sizeof(int));
*j=22;
*k=21;
cout<<"value at j = "<<*j<<"\n";
cout<<"value at k = "<<*k<<"\n";
free(k);
free(j);
cout<<"value at k = "<<*k<<"\n";
cout<<"value at j = "<<*j<<"\n";
}Last edited by big_k105; Sep 17th, 2005 at 10:31 AM. Reason: Added Code Tags |
|
|
|
|
|
#2 |
|
Expert Programmer
|
I believe you would need to use the delete operator.
delete k delete j
__________________
Join us at #programmingforums @ irc.freenode.net! My software never has bugs. It just develops random features.
|
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Use delete with new and free with malloc. In a majority of the implementations, new uses malloc, and delete will use free. There are additional things that new does over and above the malloc that are undone by delete, but not by free.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#4 |
|
Expert Programmer
|
Is this statement correct?
malloc() and free() are C. new and delete are C++.
__________________
Join us at #programmingforums @ irc.freenode.net! My software never has bugs. It just develops random features.
|
|
|
|
|
|
#5 | |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
Bjarne Stroustrup - "The C++ Programming Language"
Quote:
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty |
|
|
|
|
|
|
#6 |
|
Expert Programmer
|
Thanks stevengs, that has cleared things up for me, and probably the OP aswell.
__________________
Join us at #programmingforums @ irc.freenode.net! My software never has bugs. It just develops random features.
|
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Here is a little MS C++ compiled program:
int main (void)
{
char *myArray = new char (25);
return 0;
}/***
*new.cxx - defines C++ new routine
*
* Copyright (c) 1990-1998, Microsoft Corporation. All rights reserved.
*
*Purpose:
* Defines C++ new routine.
*
*******************************************************************************/
#include <cruntime.h>
#include <malloc.h>
#include <new.h>
#include <stdlib.h>
#ifdef WINHEAP
#include <winheap.h>
#else /* WINHEAP */
#include <heap.h>
#endif /* WINHEAP */
void * operator new( unsigned int cb )
{
void *res = _nh_malloc( cb, 1 );
return res;
}
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#8 | |
|
Programmer
Join Date: Sep 2005
Posts: 33
Rep Power: 0
![]() |
Quote:
#include <iostream.h>
#include <iostream.h>
#include <malloc.h>
class Base
{
public:
Base(){ cout<<"Constructor: Base"<<endl;}
virtual ~Base(){ cout<<"Destructor : Base"<<endl;}
};
class Derived: public Base
{
//Doing a lot of jobs by extending the functionality
public:
Derived(){ cout<<"Constructor: Derived"<<endl;}
~Derived(){ cout<<"Destructor : Derived"<<endl;}
};
void main()
{
Base *Var = new Derived;
//delete Var;
free (Var);
} |
|
|
|
|
|
|
#9 |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
My quote was merely supporting David's (DaWei) statement, but your quite welcome.
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty |
|
|
|
|
|
#10 |
|
Programmer
Join Date: Sep 2005
Posts: 33
Rep Power: 0
![]() |
Thanks to both of you
![]() |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|