![]() |
|
#1
|
||||
|
||||
Incorrect usage of free()Hey guys,
I wrote some code, and have a logical error. I wrote this code to demonstrate what I'm doing in my code : This is the phase I'm getting to : CPP / C++ / C Code:
And here I do something very bad : CPP / C++ / C Code:
Can someone point me to where I did something wrong ? Did I try to free something that doesn't belong to me ? Best regards, Kobi. __________________
It's actually a one time thing (it just happens alot). |
||||
|
#2
|
|||
|
|||
Re: Incorrect usage of free()Quote:
What compiler are you using? |
|
#3
|
||||
|
||||
Re: Incorrect usage of free()It may compile fine...but it should be
Code:
Code:
You can use malloc for q again |
|
#4
|
|||
|
|||
Re: Incorrect usage of free()Quote:
You can look it up on your favorite on-line language/library reference site. Such as http://www-ccs.ucsd.edu/c/. You will find something like the following (where I have highlighted the significant part). "realloc void *realloc(void *ptr, size_t size); The function allocates an object of size size, possibly obtaining initial stored values from the object whose address is ptr. It returns the address of the new object if successful; otherwise, it returns a null pointer. You can safely convert the return value to an object pointer of any type whose size is not greater than size. If ptr is not a null pointer, it must be the address of an existing object that you first allocate by calling calloc, malloc, or realloc. If the existing object is not larger than the newly allocated object, realloc copies the entire existing object to the initial part of the allocated object. (The values stored in the remainder of the object are indeterminate.) Otherwise, the function copies only the initial part of the existing object that fits in the allocated object. If realloc succeeds in allocating a new object, it deallocates the existing object. Otherwise, the existing object is left unchanged. If ptr is a null pointer, the function does not store initial values in the newly created object. " In other words, in your case realloc gives you a brand new space, pointed to by q and frees the space pointed to by p. Regards, Dave |
|
#5
|
|||
|
|||
Re: Incorrect usage of free()Quote:
If realloc fails for some reason, then you can never free that previously allocated block. This would be a memory leak. (A Bad Thing.) Quote:
I don't see anything ironic about it; what I see is a memory leak. If you destroy the value returned by realloc (by setting q equal to some other value), then you can't free the memory. (Still A Bad Thing.) Regards, Dave |
|
#6
|
||||
|
||||
Re: Incorrect usage of free()Thank you all.
I'll re-think my approach. Kobi. __________________
It's actually a one time thing (it just happens alot). |
|
#7
|
|||
|
|||
Re: Incorrect usage of free()Quote:
What's the problem? If you have this: CPP / C++ / C Code:
Now the variable p still points to user's space, which has now been resized. Of course if this is done inside a function, and p is used outside of the function, the argument to the function will be a pointer to p, and the pointer will be dereferenced accordingly. Everything is safe, and there are no memory leaks, assuming you free p before the program terminates. Regards, Dave |
|
#8
|
||||
|
||||
Re: Incorrect usage of free()Quote:
If realloc works fine, and the "old" allocation is deallocated, then any reference to the "old" allocation is illegal ... Now I get it. Kobi, __________________
It's actually a one time thing (it just happens alot). |
Recent GIDBlog
Problems with the Navy (Enlisted) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| FREE phpNuke Hosting: INSTANT Activation, 22 domains to pick from, Free Support +MORE | EZForum.org | Free Web Hosting | 3 | 07-May-2006 14:28 |
| Free software | ashok999 | Member Announcements, Advertisements & Offers | 0 | 28-Sep-2005 05:15 |
| Hosting Serenity - Try us for a 10 day FREE trial today! | Paradoxic | Web Hosting Advertisements & Offers | 2 | 08-May-2005 15:10 |
| 50% OFF Basic Reseller Plan - FREE Domain and DOUBLE Bandwidth - From $5 per month | Dailyhosting | Web Hosting Advertisements & Offers | 0 | 16-Mar-2005 16:06 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The