Re: malloc realloc and pointers



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

.



Relevant Pages

  • Re: trap in malloc on 2000 terminal server
    ... For the error in the debugger extension, looks like you have an old debugger ... some operation that has an undefined behavior beforehand. ... On average, heap corruption shows up as crashes in random places, ... What is the logic of malloc? ...
    (microsoft.public.win32.programmer.kernel)
  • Re: trap in malloc on 2000 terminal server
    ... For the error in the debugger extension, looks like you have an old debugger ... some operation that has an undefined behavior beforehand. ... On average, heap corruption shows up as crashes in random places, ... What is the logic of malloc? ...
    (microsoft.public.win32.programmer.tools)
  • Re: What does SZ on the ps output mean?
    ... originally assumed that no matter how many malloc and frees were done, ... there shouldn't be an upward trend. ... hope of spotting any memory leak. ... examine the java heap as mgd by the JVM. ...
    (comp.unix.solaris)
  • Re: free after realloc
    ... freefrees the memory space pointed to by ptr, ... returned by a previous call to malloc, callocor realloc(). ... If ptr is NULL, no operation is performed. ... been returned by an earlier call to malloc(), ...
    (comp.os.linux.networking)
  • Re: Modifying default Win32 heap management.
    ... I'm doing some experiment about heap management on Windows. ... is intercepting Win32 default heap management calls and modifying its ... replaced these calls with another verified malloc implementation, ... works on Win32 as well. ...
    (microsoft.public.win32.programmer.kernel)