943,446 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 62983
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 26th, 2004
0

free() = delete or delete[]?

Expand Post »
Hi there C /C++ experts...

I have a confusion regarding the function free(void *) in C and delete in C++. Suppose there is a pointer named p. In C++, we dynamically allocate memory for p to point at like this
C++ Syntax (Toggle Plain Text)
  1. p = new int [10].
,then when we use
C++ Syntax (Toggle Plain Text)
  1. delete p
, in typical situations only the memory space for p[0] gets deallocated. And when we use
C++ Syntax (Toggle Plain Text)
  1. delete[] p
p then all the memory space from p[0] to p[9] gets deallocated. I know that in C we use calloc() and malloc() to allocate memory dynamically and use free() to dellocate. Now, is this free() equivalent to delete or delete[]? That is does it deallocate memory space for only p[0] or all the memory space from p[0] to p[9]?????
Similar Threads
Reputation Points: 113
Solved Threads: 3
Posting Whiz
Asif_NSU is offline Offline
353 posts
since Apr 2004
Jun 26th, 2004
0

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

In the case you describe, delete p and delete [] p are the same as far as free() goes, but some runtimes would complain if you dont put in [].

The [] means that the compiler should call the 'vector delete' destructor, which calls the destructor on each element of the array. delete with no brackets just calls the destructor. In other words...

"delete p"

means:

p->~int(); // call the destructor of the object, in this case an int, so it may be skipped by the compiler, but you get the idea...
free(p); // free the space

"delete [] p"

means

for (int i = 0; i < (allocatedsizeof(p) / sizeof(p)); i++)
p[i].~int(); // destruct the ith object
free(p);

(allocatedsizeof() I made up to mean "the allocated memory size of the pointer()")

So the space allocated by either is always the same, its the destructor calling that is the difference. Some compilers/runtimes track whether an allocate was for a single object or an array, so you need to match your new [] with delete [], even for things like ints that don't have destructors.
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004
Jul 3rd, 2004
0

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

Do you think it actually matters?!

If you have:

C++ Syntax (Toggle Plain Text)
  1. int a=new int[8];
  2. a[0]=23;
  3. delete a;
  4. cout<<a[0];

It prints 23. The same thing applies for delete[]. Why bother deallocating it?

I have a question of my own!
If you use:

C++ Syntax (Toggle Plain Text)
  1. int *a,n,i=0;
  2. printf("%i",&n)
  3. for (i;i<n;i++)
  4. a[i]=i;
  5. for (i=0;i<n;i++)
  6. printf("%i ",a[i]);

It works very well. I DON'T get it!!! a is but a pointer and it doesn't point to anything. Even so it works. For strings, if this declaration is used: char *s,*s2; you may ask if (*s==*s2) .... You can also read it with gets and write it with puts. WHY??
Reputation Points: 16
Solved Threads: 0
Light Poster
Fili is offline Offline
34 posts
since Jun 2004
Jul 4th, 2004
0

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

It's probably bcos u r using a compilers like TURBO C/C++ IDE in DOS mode. The thing is if a pointer(automatic) is not initialized to point to something then it generally contains "garbage values". When u try to store some values to the place pointed by the pointer it actually tries to store the values in that "garbage" address. Now, run time errors will occur if u r using windows based compilers like visual c++. Bcos the place the pointer points to can be very much out of the memory space or resolution delivered to the program. Hence, an memory fault should happen.

And yes a[0] prints 23. Bcos deallocating doesnt delete everything that is contained in that "to be deallocated" memory space. What it actually does is that it makes this memory space available to other applications. If u do not free dynamically allocated memory then the space will always be kept for the program itself and this will not allow other applications to use the memory. Everytime u run a program having a dynamic allocation of memory and do not free the memory then each time u execute the program u lose certain amount of sytem memory gradually, which is referred to as memory leaking.
And even if u have "deallocated" that memory to be pointed by pointer a, the address a points to is still the same. Therefore,a[0] yields the same value, but that is bcos no other application has overwritten anything so far in the address of a[0], which can be used by applications other than ur program at anytime after it has been deallocated. Again, trying to access a[0] after the memory space for a[0] has been deallocated should result in a runtime error in any windows based compiler.

By the way, these are JUST my PURE speculation. Dont bliv anything i said unless verified by other "c/c++ techies" here. I dont even have any visual c++ or devc++. I am still doin my work in Turbo c++ IDE.
Last edited by Asif_NSU; Jul 4th, 2004 at 1:46 pm. Reason: typo
Reputation Points: 113
Solved Threads: 3
Posting Whiz
Asif_NSU is offline Offline
353 posts
since Apr 2004
Jul 4th, 2004
0

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

To chainsaw,
I havent fully understood what u said, bcos I am halfway through C and now I have to make transition to C++ bcos my course wants me to do so. I dont have crystal clear concept like how delete or delete[] works. So could u bear the pain of explaining what free does in terms of my C knowledge? that would be much appreciated.
Reputation Points: 113
Solved Threads: 3
Posting Whiz
Asif_NSU is offline Offline
353 posts
since Apr 2004
Jul 5th, 2004
-1

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

TO Asif_NSU:I must admit that in Dev C++ this doesn't work at all. BUT in the DOS version it WORKS very well. I'm using Borlamd C++ 3.1. This thing is highly useful for ME. I will be in the 8th form next year and the compilers used at the Programming Olymphiad are for DOS. Therefor this will work. To ALL C/C++ (FOR DOS) programmers WHY am I allowed to do this???????(see 3rd post) :eek:
Reputation Points: 16
Solved Threads: 0
Light Poster
Fili is offline Offline
34 posts
since Jun 2004
Jul 5th, 2004
1

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

>WHY am I allowed to do this???????

You are not allowed to do this. Undefined behavior is, well, undefined behavior. It could behave how you think is "correct", it could crash, it could something completely different.

Now I'll admit, if all undefined behavior required something hideously harsh like reformatting your hard drive, programmers would learn very quickly what not to do. But this kindness granted to us by language implementors should not be overlooked.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jul 6th, 2004
0

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

To Asif_NSU:

You asked again what free() does. Here's some code....
C++ Syntax (Toggle Plain Text)
  1. char* p1 = new char[100];
  2. char* p2 = malloc(100);
  3.  
  4. // p1 and p2 both point to 100 charactors of allocated memory. Those calls were functionally idenctical in that sense.
  5.  
  6. delete [] p1;
  7. free(p2);
  8.  
  9. // Both of those calls deleted the 100 chars of ram allocated; again they are functionally identical.
  10.  
  11. Here's where they're different:
  12.  
  13. class AThingy
  14. {
  15. public:
  16. AThingy() { printf("Constructor\n"); }
  17. ~AThingy() { printf("Destructor\n"); }
  18. };
  19.  
  20. AThingy *t1 = new AThingy[100];
  21. AThingy *t2 = (AThingy*)malloc( sizeof(AThingy) * 100 );
  22.  
  23. // now t1 and t2 both point to 100 thingys. However, the new [] call called the constructor of each of the 100 thingys, but malloc did not!
  24.  
  25. delete [] t1;
  26. free(t2);
  27.  
  28. // the delete [] called the destructor for all 100 items, the free did not!
  29.  
  30. // so malloc-->new, free-->delete.
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004
Jul 6th, 2004
0

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

Thanx again chainsaw. Now I get the idea. well, i asked one of my instructors about this, but he did not expalin it in that way. I wish u were my instructor. THANX, big time.
Reputation Points: 113
Solved Threads: 3
Posting Whiz
Asif_NSU is offline Offline
353 posts
since Apr 2004
Jul 6th, 2004
0

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

No prob! Hey, bump up my reputation a bit! I need it! :-)
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004

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