943,453 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 62987
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Jul 6th, 2004
0

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

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:
Reputation Points: 113
Solved Threads: 3
Posting Whiz
Asif_NSU is offline Offline
353 posts
since Apr 2004
Jul 8th, 2004
0

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

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:
Reputation Points: 16
Solved Threads: 0
Light Poster
Fili is offline Offline
34 posts
since Jun 2004
Aug 26th, 2009
-1

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

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;
Reputation Points: 3
Solved Threads: 0
Newbie Poster
swarop9 is offline Offline
1 posts
since Aug 2009
Oct 16th, 2009
0
Re: free() = delete or delete[]?
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
simong1024 is offline Offline
1 posts
since Oct 2009
Feb 6th, 2011
-1
Re: free() = delete or delete[]?
Thanks for the lovely post Asif_NSU and Chainsaw!
Last edited by ahtaniv; Feb 6th, 2011 at 3:52 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ahtaniv is offline Offline
9 posts
since Feb 2011

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Pro Revision...
Next Thread in C++ Forum Timeline: Marquee in C++?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC