Re: malloc realloc and pointers
- From: Kenneth Brody <kenbrody@xxxxxxxxxxx>
- Date: Thu, 29 Nov 2007 17:00:05 -0500
ravi wrote:
Hi all,
I m relatively new to C. I have few queries related to malloc():
1. When we perform malloc(), the memory allocated dynamically comes from
the heap area of the process in concern.
Technically, C doesn't define "heap". However, it is a term in
general use to mean "the memory from which malloc and friends
get their memory".
Well, we then say that the heap
has shrinked.
Not necessarily. Perhaps the amount of memory available on the
heap has shrunk, but even that's not necessarily true. It is
possible that the memory you request wasn't available in the
existing heap, and so the heap was grown to accomodate your
request. It is possible that the result is that you now have
more memory available on the heap than before the call to
malloc().
my query is: Is it that the heap physically does not
shrink but the perticular nodes are marked 'ALLOCATED' and for
subsequent calls to malloc() the memory manager remembers them and does
not reference them?
The memory management within malloc/calloc/etc. keeps track of
what memory is used and that is still available. The details on
How it does this is entirely up to the implemenation.
2. With realloc(), if some pointer 'ptr' is pointing initially to a
perticular position in a buffer (char *buffer) then on performing a
realloc() on this buffer, what will be 'ptr' pointing to?
Three possibilities:
1) realloc() returns the same pointer, in which case ptr is still
valid, pointing to the newly-sized buffer.
2) realloc() returns a different pointer, in which case ptr is
no longer valid, and must not be dereferenced.
3) realloc() returns NULL, in which case ptr is pointing to the
unchanged memory region as before.
Typically, the only use for the old pointer is to remember where
it pointed to when realloc() returns NULL. Otherwise, simply use
the new pointer.
3. whats the maximum memory size that we can allocate dynamically by
calling malloc() ?
Whatever is the maximum value of a size_t variable, though you
are more likely to run into limits of the platform you are running
on. (For example, size_t may allow values up to 4GB-1, but the
O/S may only allow 2GB.)
4. Is it valid in C to typecast a pointer? eg. code snippet... of
course int is 16 bit and long is 32 bit.
int *variable, value;
*((long*)variable)++ = value;
*((long*)variable)++ = value;
*((long*)variable)++ = value;
*((long*)variable)++ = value;
If variable is properly aligned, I believe it's valid.
--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:ThisIsASpamTrap@xxxxxxxxx>
.
- Follow-Ups:
- Re: malloc realloc and pointers
- From: K. Jennings
- Re: malloc realloc and pointers
- From: Harald van Dijk
- Re: malloc realloc and pointers
- From: Walter Roberson
- Re: malloc realloc and pointers
- References:
- malloc realloc and pointers
- From: ravi
- malloc realloc and pointers
- Prev by Date: Re: Multi precision floating point
- Next by Date: Re: Global Variables : Where are they stored ?
- Previous by thread: Re: malloc realloc and pointers
- Next by thread: Re: malloc realloc and pointers
- Index(es):
Relevant Pages
|