freeing nothing? 
Author Message
 freeing nothing?

: One question about freeing memory.
: Consider the code:

: int *p;/*don't allocate memory*/
: free(p);

: Is it legal to free memory without allocation in the first place? My view is
: that such an action will free memory that may be in use by some other parts of
: the system, hence leading to a segmentation (unix) or general protection (pc)
: fault. However, this code has never clashed on unix and only clashes
: occasionally on PC. Is my interpretation correct?

Freeing a random pointer (that is, a pointer not returned by malloc())
results in undefined behaviour.  The exception to this is free(NULL),
which does nothing.  If you free the same (valid) pointer twice, the
result is also undefined.

Will



Fri, 25 Aug 2000 03:00:00 GMT  
 freeing nothing?

Quote:

>One question about freeing memory.
>Consider the code:

>int *p;/*don't allocate memory*/
>free(p);

Assuming the declaration of p here is at block scope p is uninitialised.
The code accesses the uninitialised value of p which is an error - it is
*always* an error to access the value of an uninitialised variable (of any
type, not just pointers). So the code is broken before free is even called.
free() requires that you pass it a pointer to an object created by
malloc, calloc or realloc which has not previosuly been freed, or a null
pointer (in which case free does nothing). If you fail to do that you
get undefined behaviour - the program may do anything including crash
horribly corrupting all your files.

Quote:
>Is it legal to free memory without allocation in the first place?

No.

Quote:
>My view is
>that such an action will free memory that may be in use by some other parts of
>the system,

What will happen in that instance is unpredictable. It makes no sense to
talk about freeing memory that wasn't allocated by malloc/calloc/realloc in
the first place, that simply isn't something you can do.

Quote:
>hence leading to a segmentation (unix) or general protection (pc)
>fault.

That is a likely outcome most likely becoase you've caused malloc's
datastructures to be corrupted.

Quote:
>However, this code has never clashed on unix and only clashes
>occasionally on PC. Is my interpretation correct?

That is pure luck. Why would you want to ``free'' memory not allocated
through malloc/calloc/realloc anyway?

--
-----------------------------------------


-----------------------------------------



Fri, 25 Aug 2000 03:00:00 GMT  
 freeing nothing?


Quote:
> int *p;/*don't allocate memory*/
> free(p);

> Is it legal to free memory without allocation in the first place?

No. The value of 'p' is indeterminate. Using it in any way invokes undefined
behavior. Your hard drive will speedily be overwritten with the repeating
integral pattern 0xDEADBEEF. Have a nice day.

--
(initiator of the campaign for grumpiness where grumpiness is due in c.l.c)

Attempting to write in a hybrid which can be compiled by either a C compiler
or a C++ compiler produces a compromise language which combines the drawbacks
of both with the advantages of neither.



Fri, 25 Aug 2000 03:00:00 GMT  
 freeing nothing?

Good OSes should produce an segmentation fault since is not allowed to
free memory without allocation. *p shopuld also point to NULL.
Sometimes you could pass this statement without a SF.

1.) Godd free implementation. free checks if the pointer points to a
good adrress and do nothing when a wrong pointer is passed.
2.) It could be thta P points to a good address. So this memory would
be set free.



Sat, 26 Aug 2000 03:00:00 GMT  
 freeing nothing?

Quote:

> One question about freeing memory.
> Consider the code:

> int *p;/*don't allocate memory*/
> free(p);

> Is it legal to free memory without allocation in the first place?

Definitely not.

Quote:
> My view is that such an action will free memory that may be in use by some
> other parts of the system, hence leading to a segmentation (unix) or general
> protection (pc) fault.

Not very likely. malloc() will maintain a list of allocated memory
and free() will use this information to free the memory. There will
be a separate list for each process in memory, passing a garbage
address to free() will simply mean that free() cannot find the
memory to be freed in the internal list. It may crash, it may
even decide to format the hard disk, but it can't free memory
used by another process.

Of course this is all system dependent. I'm not saying you
couldn't make an operating system where this happened, just
that it wouldn't make sense to do so.

On second thoughts, maybe you *can* do it under MS-DOS...

--
<\___/>
/ O O \
\_____/ FTB.



Mon, 28 Aug 2000 03:00:00 GMT  
 freeing nothing?

Quote:

>One question about freeing memory.
>Consider the code:

>int *p;/*don't allocate memory*/
>free(p);

>Is it legal to free memory without allocation in the first place?

Others have answered this in detail: short answer, "no".

Quote:
>However, this code has never clashed on unix and only clashes
>occasionally on PC. Is my interpretation correct?

One point to bear in mind is that static pointers (e.g. those declared
outside all functions) are initialised to NULL by default.  Since
'free(NULL)' is permitted and does nothing, for a static 'p' the above
is legal.

It's also, of course, possible that the undefined behaviour caused by
using the value of 'p' (when it's automatic and uninitialised) just
happens to manifest itself by 'p' being NULL.

Cheers,
Richard
--
Richard Stamp
Churchill College, Cambridge



Mon, 28 Aug 2000 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Data Relationships- Code says nothing has changed

2. Karim Mustapha Binalshibh: next Al Queda attack on Buy Nothing Day

3. Sadam Fahd Atef: next Al Queda attack on Buy Nothing Day

4. Pervez AL-Jabr: next Al Queda attack on Buy Nothing Day

5. Youssef Ghassan al Makhoul: Al Queda will destroy the infidels (next attack on Buy Nothing Day)

6. Nothing showing up...

7. Create DataSet Thru Code From Nothing?

8. fprintfing to nothing

9. initialize ComboBox to select nothing

10. AcceptButton does nothing?

11. while nothing

 

 
Powered by phpBB® Forum Software