Re: malloc realloc and pointers
- From: Peter Nilsson <airia@xxxxxxxxxxx>
- Date: Thu, 29 Nov 2007 13:44:58 -0800 (PST)
ravi <nos...@xxxxxxxxxxxxxx> wrote:
Hi all,
I m relatively new to C.
Then why ask questions on how it's implemented? You're
trying to run before you can crawl.
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.
If you say so. The language standard does not specify how
malloc is implemented, merely how it must operate.
Well, we then say that the heap has shrinked. 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?
You should ask in a group where such implementation details
are topical.
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?
The original pointer value becomes indeterminate if the
allocation is successful.
3. whats the maximum memory size that we can allocate
dynamically by calling malloc() ?
The most you can try for is ((size_t) -1). The most you
can actually allocate depends on the implementation.
4. Is it valid in C to typecast a pointer?
Yes, but subject to a few rules. Converting between
different non-void types isn't as well defined as
some program code would have you believe.
eg. code snippet... of
course int is 16 bit and long is 32 bit.
If that has any relevance, then you're heading down the
wrong path in your quest to learn C.
int *variable, value;
*((long*)variable)++ = value;
This potentially breaks several rules on pointer conversion.
It also violates a constraint.
The cast from int * to long * is not required to be well
defined. Even if it is, it will not produce a modifiable
lvalue suitable for postfix ++.
--
Peter
.
- Follow-Ups:
- Re: malloc realloc and pointers
- From: Nick Keighley
- 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: Generalized function for deallocating and setting pointer to NULL
- Previous by thread: malloc realloc and pointers
- Next by thread: Re: malloc realloc and pointers
- Index(es):
Relevant Pages
|
|