Re: memory allocation wrapper
- From: "Malcolm" <regniztar@xxxxxxxxxxxxxx>
- Date: Sat, 19 Aug 2006 11:36:44 +0100
"goose" <lkman@xxxxxxxxxxxxx> wrote in message
Hello allThere's a much easier way of achieveing what you want to do.
I've written a wrapper for malloc and friends. Its
available from
http://www.lelanthran.com/downloads/os_mem/index.php
The reason for doing writing this so that newbies
can finally get answers to the following
newbie questions:
1. How do I know how much memory a pointer points to?
2. How can I tell if a pointer is valid?
I know most of the regs don't need this, but be
a good samaritan and point out the bugs :-)
Comments and criticisms(sp?) welcome; note that it aims
for c99 compliance only.
goose,
Simply put a structure at the start of the allocated memory, add your
control information, and return the pointer immediately after.
Then when the user calls the free, subtract the size of the control
structure, and free. Similarly for the access functions.
There is a niggly snag. malloc() must return memory aligned for any purpose.
In practise you can get "good enough" portability by padding your structure
out to an even number of doubles. In standards land, you risk the horrors of
UB, and there is no way of achieving a fully compliant program in ANSI C.
--
www.personal.leeds.ac.uk/~bgy1mm
freeware games to download.
.
- Follow-Ups:
- Re: memory allocation wrapper
- From: goose
- Re: memory allocation wrapper
- References:
- memory allocation wrapper
- From: goose
- memory allocation wrapper
- Prev by Date: Re: What ever happened to?
- Next by Date: Re: float? double?
- Previous by thread: memory allocation wrapper
- Next by thread: Re: memory allocation wrapper
- Index(es):
Relevant Pages
|