Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 17th, 2005, 10:29 AM   #1
aloksave
Programmer
 
aloksave's Avatar
 
Join Date: Sep 2005
Posts: 33
Rep Power: 0 aloksave is on a distinguished road
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
aloksave is offline   Reply With Quote
Old Sep 17th, 2005, 10:45 AM   #2
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 4 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
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.
coldDeath is offline   Reply With Quote
Old Sep 17th, 2005, 10:55 AM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Sep 17th, 2005, 11:06 AM   #4
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 4 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
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.
coldDeath is offline   Reply With Quote
Old Sep 17th, 2005, 11:07 AM   #5
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
Bjarne Stroustrup - "The C++ Programming Language"

Quote:
From C, C++ inherited a functional interface to dynamic memory. It can be found in <cstdlib>.

void* malloc(size_t, s);
void* calloc(size_t n, size_t s);
void free(void* p)
void* realloc( void* p, size_t s);

These functions should be avoided in favor of new, delete, and standard containers. In particular, free() does not invoke destructors for the memory it frees. An implementation of new and delete may use these functions, but there is no guarantee that it does. For example, allocating an object using new and deleting it using free() is asking for trouble.
__________________
-Steven
"Is this a piece of your brain?" - Basil Fawlty
stevengs is offline   Reply With Quote
Old Sep 17th, 2005, 11:09 AM   #6
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 4 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
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.
coldDeath is offline   Reply With Quote
Old Sep 17th, 2005, 11:45 AM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Here is a little MS C++ compiled program:
int main (void)
{
   char *myArray = new char (25);

   return 0;
}
Here is what that line invokes:
/***
*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;
}
"nh_malloc" is the "new handler malloc." It checks to see whether the ordinary or the debug heap allocation method is in order and makes the appropriate further calls.
__________________
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
DaWei is offline   Reply With Quote
Old Sep 17th, 2005, 12:21 PM   #8
aloksave
Programmer
 
aloksave's Avatar
 
Join Date: Sep 2005
Posts: 33
Rep Power: 0 aloksave is on a distinguished road
Quote:
Originally Posted by stevengs
Bjarne Stroustrup - "The C++ Programming Language"
Thanks a lot...very true,free doesnt call the destructors...just checked out with the ex below...

#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);
}
aloksave is offline   Reply With Quote
Old Sep 17th, 2005, 12:38 PM   #9
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
My quote was merely supporting David's (DaWei) statement, but your quite welcome.
__________________
-Steven
"Is this a piece of your brain?" - Basil Fawlty
stevengs is offline   Reply With Quote
Old Sep 17th, 2005, 12:41 PM   #10
aloksave
Programmer
 
aloksave's Avatar
 
Join Date: Sep 2005
Posts: 33
Rep Power: 0 aloksave is on a distinguished road
Thanks to both of you
aloksave is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:02 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC