Re: malloc realloc and pointers
- From: CBFalconer <cbfalconer@xxxxxxxxx>
- Date: Thu, 29 Nov 2007 19:42:25 -0500
ravi wrote:
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. 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?
There is not necessarily anything called a heap. That is an
implementors decision. Somehow or other the implementor guarantees
that subsequent malloc calls do not return pointers to previously
allocated space (unless freed).
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?
That depends. If realloc succeeds, it returns a replacement value
for ptr, which has the desired space and preserves the original
data. If realloc fails it return NULL, and ptr and *ptr are both
unaltered.
3. whats the maximum memory size that we can allocate dynamically by
calling malloc() ?
Depends on the system. All you can find out is that a malloc
(etc.) request is fulfilled or rejected.
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;
Very likely to cause serious run-time faults. Don't do it.
--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.
--
Posted via a free Usenet account from http://www.teranews.com
.
- Follow-Ups:
- Re: malloc realloc and pointers
- From: Flash Gordon
- Re: malloc realloc and pointers
- References:
- malloc realloc and pointers
- From: ravi
- malloc realloc and pointers
- Prev by Date: Re: pointers as parameters
- Next by Date: Re: pointer as argument and parameter
- Previous by thread: Re: malloc realloc and pointers
- Next by thread: Re: malloc realloc and pointers
- Index(es):
Relevant Pages
|