Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > malloc realloc and pointers

Reply
Thread Tools

malloc realloc and pointers

 
 
ravi
Guest
Posts: n/a
 
      11-29-2007
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. 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?

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?

3. whats the maximum memory size that we can allocate dynamically by
calling malloc() ?

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;

thanx in advance

 
Reply With Quote
 
 
 
 
Peter Nilsson
Guest
Posts: n/a
 
      11-29-2007
ravi <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Kenneth Brody
Guest
Posts: n/a
 
      11-29-2007
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: <(E-Mail Removed)>

 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      11-29-2007
In article <(E-Mail Removed)>,
Kenneth Brody <(E-Mail Removed)> wrote:
>ravi wrote:


>> 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.


variable has not been initialized, so it is a dangling pointer.
--
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth
 
Reply With Quote
 
Harald van Dijk
Guest
Posts: n/a
 
      11-29-2007
On Thu, 29 Nov 2007 17:00:05 -0500, Kenneth Brody wrote:
> ravi wrote:
>> 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.


According to DR #260, applied to realloc, even if memcmp proves the new
pointer is identical to the old pointer, you're not allowed to use them
interchangeably. If you pass a non-null pointer ptr to realloc, and it
doesn't return a null pointer, ptr is no longer valid.

(I do not believe the DR's conclusions can be drawn from any
interpretation of the actual standard, and in fact I believe some points
from the DR directly contradict explicit guarantees by the standard, but
that's just me.)
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      11-30-2007
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

 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      11-30-2007
On Thu, 29 Nov 2007 21:50:38 +0100 (CET), ravi <(E-Mail Removed)>
wrote in comp.lang.c:

> 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. 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?
>
> 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?
>
> 3. whats the maximum memory size that we can allocate dynamically by
> calling malloc() ?
>
> 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;


This is not legal C code. The cast generates an rvalue, and the post
increment operator cannot be applied to lvalues. You cannot assign to
the result of a cast.

> *((long*)variable)++ = value;
> *((long*)variable)++ = value;
> *((long*)variable)++ = value;
>
> thanx in advance


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
Reply With Quote
 
K. Jennings
Guest
Posts: n/a
 
      11-30-2007
On Thu, 29 Nov 2007 17:00:05 -0500, Kenneth Brody wrote:

> ravi wrote:
>> 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.


What happens when ptr is not one returned by a previous
invocation to malloc(), calloc() or realloc()?

 
Reply With Quote
 
cr88192
Guest
Posts: n/a
 
      11-30-2007

"K. Jennings" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> On Thu, 29 Nov 2007 17:00:05 -0500, Kenneth Brody wrote:
>
>> ravi wrote:
>>> 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.

>
> What happens when ptr is not one returned by a previous
> invocation to malloc(), calloc() or realloc()?
>


then, if you are the program, ninjas run up, kick you somewhere painful, and
insert a corncob somewhere painful.
one then has to try to hobble off to the bathroom for a 'core dump'...



 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      11-30-2007
"K. Jennings" wrote:
>

.... snip ...
>>
>> 3) realloc() returns NULL, in which case ptr is pointing to the
>> unchanged memory region as before.

>
> What happens when ptr is not one returned by a previous
> invocation to malloc(), calloc() or realloc()?


Then you know that realloc moved the storage. It doesn't affect
the data held by the pointed to object.

--
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Different sizes of data and function pointers on a machine -- void*return type of malloc, calloc, and realloc Myth__Buster C Programming 23 06-26-2012 08:29 PM
A Question about new vs malloc and realloc. DrBob C++ 2 11-26-2003 10:17 PM
Problem with malloc, realloc, _msize and memcpy Bren C++ 8 09-03-2003 11:01 PM
Re: Override malloc,calloc,realloc and free? Dan Pop C Programming 0 06-26-2003 04:52 PM
Re: Override malloc,calloc,realloc and free? Jun Woong C Programming 0 06-26-2003 03:49 PM



Advertisments