473,385 Members | 1,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

More questions on realloc

Thanks to everybody who provided an answer to my previous question on
this. I am trying to grasp the nitty-gritty details of this, and I am
having difficulties understanding some of the issues involved.

According to the man page (and I quote)

realloc(void *ptr, size_t size) changes the size of the memory block
pointed to by ptr to size bytes. The contents will be unchanged to the
minimum of the old and new sizes; newly allocated memory will be
uninitialized.

Let's assume the following sequence (my apologies for the sloppiness of
my language in what follows; I am taking for granted that experienced C
insiders will have no difficulty cutting through it):

p = malloc(size1) ;

/* Code to copy some stuff to the memory area pointed to by p */

q = realloc(p, size2) ;

For simplicity, I will assume that q != p. With this, my understanding is
that realloc will copy the contents of p to q, to the appropriate length,
and will then free p.

My question is, how does realloc know that p points to a memory region
size1 bytes in length? Or, to put it differently, can realloc be
implemented with malloc, and no other memory management call?


Nov 29 '05 #1
5 1790
On Tue, 29 Nov 2005 17:01:17 GMT, in comp.lang.c , "James S.
Singleton" <pt***@excite.com> wrote:
My question is, how does realloc know that p points to a memory region
size1 bytes in length?
By magic.

Seriously, this is an implementation detail that the end user doesn't
actually need to know. Under the hood it might be doing all sorts of
things (a lookup table of malloced blocks and their sizes, storing the
size in memor just before the malloced block, etc etc. But you don't
need to know - it just works.
Or, to put it differently, can realloc be
implemented with malloc, and no other memory management call?


You'd need memcpy and free. How else can you copy the memory from the
old to the new pointers, and then free the old one?

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 29 '05 #2
Mark McIntyre wrote:
On Tue, 29 Nov 2005 17:01:17 GMT, in comp.lang.c , "James S.
Singleton" <pt***@excite.com> wrote:

My question is, how does realloc know that p points to a memory region
size1 bytes in length?

[snip]
Or, to put it differently, can realloc be
implemented with malloc, and no other memory management call?

You'd need memcpy and free. How else can you copy the memory from the
old to the new pointers, and then free the old one?

But realloc doesn't always have to reallocate memory; if you malloc,
say, 10 bytes, you may well actually get 32 (or 1024) bytes. Your C
library knows how much but you don't, so calling realloc() is often a
win over malloc(), memcpy() and free().

Robert
Nov 29 '05 #3


James S. Singleton wrote On 11/29/05 12:01,:
Thanks to everybody who provided an answer to my previous question on
this. I am trying to grasp the nitty-gritty details of this, and I am
having difficulties understanding some of the issues involved.

According to the man page (and I quote)

realloc(void *ptr, size_t size) changes the size of the memory block
pointed to by ptr to size bytes. The contents will be unchanged to the
minimum of the old and new sizes; newly allocated memory will be
uninitialized.

Let's assume the following sequence (my apologies for the sloppiness of
my language in what follows; I am taking for granted that experienced C
insiders will have no difficulty cutting through it):

p = malloc(size1) ;

/* Code to copy some stuff to the memory area pointed to by p */

q = realloc(p, size2) ;

For simplicity, I will assume that q != p. With this, my understanding is
that realloc will copy the contents of p to q, to the appropriate length,
and will then free p.

My question is, how does realloc know that p points to a memory region
size1 bytes in length? Or, to put it differently, can realloc be
implemented with malloc, and no other memory management call?


The memory-management functions keep track of the
sizes of allocated and available blocks of memory. The
details of how this is done differ from one implementation
to another. One frequently-used method is to allocate a
little bit more memory than is requested, plant some
housekeeping data at the beginnning, and return a pointer
to the tail end. Later, free() or realloc() can look "just
before" the pointer they're given to find the housekeeping
data again.

... but that's just one way; others exist. From the
programmer's perspective, it's all "implementation magic,"
just like the machinery behind, say, fopen(). You don't
(usually) care how the spell is cast, just that the magic
happens.

--
Er*********@sun.com

Nov 29 '05 #4
On Tue, 29 Nov 2005 17:27:48 GMT, in comp.lang.c , Robert Harris
<ro*************@blueyonder.co.uk> wrote:
Mark McIntyre wrote:
On Tue, 29 Nov 2005 17:01:17 GMT, in comp.lang.c , "James S.
Singleton" <pt***@excite.com> wrote:
Or, to put it differently, can realloc be
implemented with malloc, and no other memory management call?


You'd need memcpy and free. How else can you copy the memory from the
old to the new pointers, and then free the old one?

But realloc doesn't always have to reallocate memory; if you malloc,
say, 10 bytes, you may well actually get 32 (or 1024) bytes.


This is sometimes true, but not relevant to the OP since
a) you don't know if your compiler can do this and
b) you don't know if it did, in any specific case and
c) it may in any events not be able to, in any specific case.

Whatever, realloc can under the hood operate however it likes,
provided it behaves *as if* it had done a new *alloc, copy and free.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 29 '05 #5
"James S. Singleton" <pt***@excite.com> writes:
Let's assume the following sequence (my apologies for the sloppiness of
my language in what follows; I am taking for granted that experienced C
insiders will have no difficulty cutting through it):

p = malloc(size1) ;

/* Code to copy some stuff to the memory area pointed to by p */

q = realloc(p, size2) ;

For simplicity, I will assume that q != p. With this, my understanding is
that realloc will copy the contents of p to q, to the appropriate length,
and will then free p.

My question is, how does realloc know that p points to a memory region
size1 bytes in length? Or, to put it differently, can realloc be
implemented with malloc, and no other memory management call?


As others already have said, it's an implementation detail. My guess is that the
most common scheme is to have the size of a memory block stored immediatly before
the block in question. This is also the reason why writing beyond the end of an
allocated block can have so strange effects, crashing a program much later in
a totally different context, making it difficult to debug.

K & R has a section describing a typical memory allocator. I recommend you to
read that.

Realloc could be implemented in terms of malloc, free, and memcpy, but you
would lose the possibility to extend an existing memory block without the
need of copy in the case that there are enough free memory adjacent to the
current block. Note that it is trivial to implement both malloc and free in
terms of realloc:

void* malloc(size_t sz) { return realloc(NULL, sz); }
void free(void* ptr) { realloc(ptr, 0); }

/Niklas Norrthon
Nov 30 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Michael | last post by:
How would I go about shrinking the buffer that was allocated with new, or expanding it in place? I basically need a realloc equivalent for new. Thanks in advance. Michael
2
by: shilpi | last post by:
Hi, I am trying to allocate mroe memory if the following condition is true. But I get Invalid pointer write memory error when running this code through purifier.It is not freeing up oldlevels Any...
86
by: Walter Roberson | last post by:
If realloc() finds it necessary to move the memory block, then does it free() the previously allocated block? The C89 standard has some reference to undefined behaviour if one realloc()'s memory...
10
by: James S. Singleton | last post by:
Thanks everybody for your replies. I gather that: a) How to obtain the size of the memory region pointed to by ptr in realloc(ptr, size) is implementation-dependent. b) Implementing...
27
by: fctk | last post by:
hello, i have some questions. 1) do constant expressions include string literals? for example, is "hello, world" a constant expression? 2) int i = 0; is the equal sign the assignment...
9
by: bwaichu | last post by:
I am writing a function that: 1) checks the size of the a non c string being copied against the available buffer space; len should not be large enough to contain a nul terminator (should I...
19
by: ivan.leben | last post by:
Let's say I have a piece of allocated memory which I want to expand and reuse if possible or allocate in a different part of RAM if resizing is not possible, however, in the latter case I don't...
12
by: subramanian | last post by:
I have taken the following prototype from K & R. void *realloc(void *p, size_t size); Suppose p was earlier allocated by malloc. Suppose I am calling realloc with larger size value. If...
4
by: Kenneth Brody | last post by:
I looked at my copy of n1124, and I didn't see anything about this particular situation... What happens if you realloc() to a size of zero? Implementations are allowed to return NULL on...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.