Re: malloc realloc and pointers



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
.



Relevant Pages

  • Re: A solution for the allocation failures problem
    ... It is not possible to check EVERY malloc result within complex software. ... allocate a big buffer that is not used ... a memory exhaustion situation arises, ... What if no allocation fails? ...
    (comp.lang.c)
  • Re: SIGSEGV handling
    ... times out of forty-two -- a crash in malloc(), free, realloc, ... overrunning an allocation and scribbling on the un-allocated bytes ... miscalculating string sizes isn't the only way to ... overrun a buffer: There are lots of other ways to make off-by-one ...
    (comp.unix.programmer)
  • [REVS] Heap Off by One - Explained
    ... First of all we have to know what a malloc chunk is or at least how it ... As we can see the 0x11 is the minimal allocation. ... buffer, and using a carefully calculated length we can set to zero the ...
    (Securiteam)
  • [patch 1/2] x86, ptrace: support pebs in ds.c
    ... BTS and PEBS recording. ... This used to be implicit on a change of the requested buffer size. ... * guarding context and buffer memory allocation. ...
    (Linux-Kernel)
  • [patch 1/2] x86, ptrace: support pebs in ds.c
    ... BTS and PEBS recording. ... This used to be implicit on a change of the requested buffer size. ... * guarding context and buffer memory allocation. ...
    (Linux-Kernel)