free() = delete or delete[]?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2004
Posts: 353
Reputation: Asif_NSU is on a distinguished road 
Solved Threads: 3
Asif_NSU's Avatar
Asif_NSU Asif_NSU is offline Offline
Posting Whiz

Re: free() = delete or delete[]?

 
0
  #11
Jul 6th, 2004
TechTalkForums Message
You must spread some Reputation around before giving it to Chainsaw again.





I did try to bump up ur reputation . But that's the message i got . So, wait till I find someone else worthy of some thumbs up.:cheesy:
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 34
Reputation: Fili is an unknown quantity at this point 
Solved Threads: 0
Fili's Avatar
Fili Fili is offline Offline
Light Poster

Re: free() = delete or delete[]?

 
0
  #12
Jul 8th, 2004
To Dave: Thanks a lot. I've figured out why it sometimes would work and other times it would crash! When a pointer is declared in main(), it has a residual adress to a memory block. Therefore, it can be used BUT if more such pointers exist(uninitialized) the result might be that one overwrites the other etc. etc. or it may actually get the text or variables entered in the console before (C gets all information from a place in the PC's memory where all keys are stored->there is a pointer to that mem. block) So, NEVER EVER use a pointer to a mem. block BEFORE allocating it :mad:
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 1
Reputation: swarop9 has a little shameless behaviour in the past 
Solved Threads: 0
swarop9 swarop9 is offline Offline
Newbie Poster

Re: free() = delete or delete[]?

 
-1
  #13
Aug 26th, 2009
free() works similar to both delete and delete[] according to the sittuation it is used.

for example consider:

int *p=(int*)malloc(sizeof(int));
free(p); //de-allocates memory of one integer size {i.e, sizeof(int);}
// works similar to delete p


int *p=(int*)malloc(sizeof(int*15)); //here 15 is an arbirtrary size
free(p); //de-allocates memory of 15 integers size {i.e, sizeof(int*15);}
//works similar to delete [] p;
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 1
Reputation: simong1024 is an unknown quantity at this point 
Solved Threads: 0
simong1024 simong1024 is offline Offline
Newbie Poster
 
0
  #14
Oct 16th, 2009
I hope this does more to help than to confuse, but here goes another answer. The original question was; which is the same as free(), delete or delete[]? It turns out that none of the three are equal. free() simply "returns" the block of memory back to the pool of available memory while delete "knows" if a destructor needs to be called first. delete[] will call the destructor for each object in an array if needed, before "returning" the memory to the pool of available memory. It is also important to match calls of malloc() with free(), new with delete and new[] with delete[].

Just to point some thing s out, posting #3 is filled with unpredictable behavior. The fact that it's unpredictable is why the different compilers don't react to it the same.

1. a could start out pointing to any memory address (in the stack, in the heap, anywhere).

2. The code will then print out the address of n, not the value. (forgot the semicolon )

3. n could be any integer value, there's no telling how many times this thing is going to loop for.

4. now "a[i] = i" is just writing to some location in memory. Hopefully it doesn't write over anything important like a return address.

5. It's a long shot, but possible that "a[i] = i" wrote over the value of n. This may loop a different number of times from before.

6. Most of the time, this will print out the numbers 0 1 2 3... for however many n now holds the value of.

This code also "works" for strings because strings are just arrays of 1 or 2 byte integers (char or wchar_t respectively) in c/c++. It has little chance of actually working with the desired string length in any way though.

Hopefully this sheds some light on what this code is doing.
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC