Re: malloc realloc and pointers



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>

.



Relevant Pages

  • Re: sizeof(ptr) = ?
    ... The value returned by malloc() is of type 'void*', ... The memory is typeless until an object has been written ... Since 'void' is defined to be an incomplete ... an lvalue of a complete type, there must be a pointer conversion ...
    (comp.lang.c)
  • malloc realloc and pointers
    ... When we perform malloc(), the memory allocated dynamically comes from ... the heap area of the process in concern. ... perticular position in a buffer then on performing a ...
    (comp.lang.c)
  • Re: stack and heap
    ... books explain that local stack variables for each function are automatically allocated when function starts and deallocated when it exits. ... malloc() always takes memory in the heap. ...
    (comp.programming)
  • Re: memory allocation wrapper
    ... I've written a wrapper for malloc and friends. ... The reason for doing writing this so that newbies ... How do I know how much memory a pointer points to? ...
    (comp.lang.c)
  • Re: 2D array of structures
    ... Don't cast the return value of malloc(), ... you allocate here memory for 7 such structures. ... a pointer to the start of this memory, which is of type 'STRUCTURE *' ...
    (comp.lang.c)