473,407 Members | 2,546 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,407 software developers and data experts.

Is realloc good form ?

Hello all,
I have a 'good practice' question.
Lately I've been using a lot of functions such as this:

void Func(int Size, double *Array) {
static double *Transformed=NULL;
Transformed=realloc(Transformed, Size*sizeof(double));
// Do something from Array to Transformed
}

Now if the value of Size changes a lot between calls, the resulting prog is
poorly optimized (it reallocates each time). I'm ok with that.
Ignoring the fact that the memory is never freed, if the value of Size
changes seldom, does the call to realloc wastes time then ?
--
Guillaume Dargaud
http://www.gdargaud.net/
Oct 18 '07 #1
9 2306
On Oct 18, 10:02 am, "Guillaume Dargaud"
<use_the_form_on_my_contact_p...@www.gdargaud.netw rote:
Hello all,
I have a 'good practice' question.
Lately I've been using a lot of functions such as this:

void Func(int Size, double *Array) {
static double *Transformed=NULL;
Transformed=realloc(Transformed, Size*sizeof(double));
// Do something from Array to Transformed

}

Now if the value of Size changes a lot between calls, the resulting prog is
poorly optimized (it reallocates each time). I'm ok with that.
Ignoring the fact that the memory is never freed, if the value of Size
changes seldom, does the call to realloc wastes time then ?
It depends. realloc will often be clever enough to realise that it can
just use the previous block of data without any change. So if you pass
in the same Size or similar Size values (like 10000, 10001, 9999) it
will be fast. However, if a new block is allocated, all the data will
be copied from the previous block to the new block, because that is
what realloc does. That might be quite wasteful.

You might just add another variable "static int allocatedSize = 0",
compare new and old size and only realloc when it gets bigger.

Oct 18 '07 #2
On Oct 18, 1:28 pm, "christian.bau" <christian....@cbau.wanadoo.co.uk>
wrote:
It depends. realloc will often be clever enough to realise that it can
just use the previous block of data without any change. So if you pass
in the same Size or similar Size values (like 10000, 10001, 9999) it
will be fast. However, if a new block is allocated, all the data will
be copied from the previous block to the new block, because that is
what realloc does. That might be quite wasteful.
realloc tries not to copy the data, just resize the available block of
memory.
Note that realloc() *may* move the memory allocation resulting in a different return value than ptr.
Oct 18 '07 #3
On Oct 18, 11:28 am, "christian.bau"
<christian....@cbau.wanadoo.co.ukwrote:
On Oct 18, 10:02 am, "Guillaume Dargaud"

<use_the_form_on_my_contact_p...@www.gdargaud.netw rote:
Hello all,
I have a 'good practice' question.
Lately I've been using a lot of functions such as this:
void Func(int Size, double *Array) {
static double *Transformed=NULL;
Transformed=realloc(Transformed, Size*sizeof(double));
// Do something from Array to Transformed
}
Now if the value of Size changes a lot between calls, the resulting prog is
poorly optimized (it reallocates each time). I'm ok with that.
Ignoring the fact that the memory is never freed, if the value of Size
changes seldom, does the call to realloc wastes time then ?
If you're ok with the fact that the function calls realloc each time,
then which waste of time are you worried about ?

Leaving your question aside there are a couple of problems with the
function. It would be better to make Size of type size_t. You need to
make sure that the expression Size*sizeof(double) does not overflow
before you pass it to realloc.
You might just add another variable "static int allocatedSize = 0",
compare new and old size and only realloc when it gets bigger.
I second that apart from the fact that allocatedSize also needs to be
size_t.

Oct 18 '07 #4
vi*************@gmail.com wrote:
realloc tries not to copy the data, just resize the available block of
memory.
Does the standard enforce this? Otherwise you are simply discussing what
some (possibly all, but I doubt you could prove that) implementations do.
Oct 18 '07 #5
Mark Bluemel said:
vi*************@gmail.com wrote:
>realloc tries not to copy the data, just resize the available block of
memory.

Does the standard enforce this?
No.
Otherwise you are simply discussing what
some (possibly all, but I doubt you could prove that) implementations do.
Right.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 18 '07 #6
vi*************@gmail.com wrote:
"christian.bau" <christian....@cbau.wanadoo.co.ukwrote:
>It depends. realloc will often be clever enough to realise that
it can just use the previous block of data without any change.
So if you pass in the same Size or similar Size values (like
10000, 10001, 9999) it will be fast. However, if a new block is
allocated, all the data will be copied from the previous block
to the new block, because that is what realloc does. That might
be quite wasteful.

realloc tries not to copy the data, just resize the available
block of memory.
That depends on the malloc/free/realloc package, and is not
guaranteed. One package that goes to lengths to avoid copying is
nmalloc for DJGPP and similar, available at:

<http://cbfalconer.home.att.net/download/>

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Oct 19 '07 #7
On Oct 19, 8:32 pm, Keith Thompson <ks...@mib.orgwrote:
Also, even if you try to reallocate the same size, I can imagine
realloc() noticing that the chunk is in the middle of a region of
mostly free space, and that it can help future allocations by
relocating it so it's adjacent to other allocated space. I don't know
whether any actual allocators do this.
I have used implementations used for debugging that would _always_
allocate a new block on purpose. That makes bugs where someone didn't
expect a changed pointer more obvious.
Oct 20 '07 #8
"christian.bau" wrote:
>
On Oct 19, 8:32 pm, Keith Thompson <ks...@mib.orgwrote:
Also, even if you try to reallocate the same size, I can imagine
realloc() noticing that the chunk is in the middle of a region of
mostly free space, and that it can help future allocations by
relocating it so it's adjacent to other allocated space. I don't know
whether any actual allocators do this.

I have used implementations used for debugging that would _always_
allocate a new block on purpose. That makes bugs where someone didn't
expect a changed pointer more obvious.
Sounds reasonable, especially if the old block's contents are trashed
at the same time.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Oct 20 '07 #9
Kenneth Brody wrote:
>
.... snip ...
>
I have seen implementations take a shrinking realloc(), and merge
the released space with an adjoining free chunk, creating a single,
larger chunk. (I would suspect that this is relatively common.)
Here is an extraction from nmalloc.c, with most code removed,
showing the cases handled to avoid any unnecessary memory copying.

/* if decreasing simply reduce size and move excess to free */
else if (szneed ((ulong)(INT_MAX - 65536))) {
/* reject excessive size request */
p = NULL; goto exeunt;
}
else if (ISFREE(m->next) &&
(szneed <= (m->sz + m->next->sz)) ) {
/* the 'next' block is free and adequate so use it */
/* else m is the oversized return block */
}
else if ((lastsbrk == m->next) &&
((szneed + MINSAVE) <= (m->sz + lastsbrk->sz)) ) {
/* lastsbrk is adequate and adjacent so use it */
}
else if (ISFREE(m->prev) &&
(szneed <= (m->sz + m->prev->sz)) ) {
/* the 'prev' block is free and adequate so use it */
}
else if ((b = searchfree(szneed))) {
/* An adequate free block exists, copy over, free old */
}
else if (lastsbrk &&
((szneed + MINSAVE) <= lastsbrk->sz) ) {
DBGPRTR(EOL " Realloc is copying into lastsbrk");
}
/* else malloc new size, copy data, and free old */
else if ((m1 = extendsbrk(szneed))) {
if (lastsbrk == m->next) {
DBGPRTR(EOL " Realloc is now using lastsbrk extended");
}
else {
/* At this point lastsbrk is adequate size */
/* split off, copy over, and free old */
}
}
else m = NULL; /* failure */

You can find the complete code on my site (URL in sig).

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com

Oct 22 '07 #10

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

Similar topics

9
by: WL | last post by:
Hey, all. I'm creating an array of strings (char **argv style) on the fly, and using realloc to create string pointers, and malloc for the strings itself (if that makes any sense). I'm using the...
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...
5
by: PCHOME | last post by:
Hi! I need to implement a matrix M which has the form: double M; , but the m and n are only known on RUN time. I am not good in C, I am thinking to use:
7
by: Jonathan Shan | last post by:
Hello all, I am trying to run a program which has dynamic array of type struct. The program works until the line which uses realloc function to allocate more memory. I have tried to reproduce...
28
by: bwaichu | last post by:
Is it generally better to set-up a buffer (fixed sized array) and read and write to that buffer even if it is larger than what is being written to it? Or is it better to allocate memory and...
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...
37
by: ravi.cs.2001 | last post by:
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....
29
by: marvinla | last post by:
Hello! I'm a beginner in C, and I'm having trouble with a pointer-to-pointer reallocation. This piece of code works well, but Valkyrie warns some parts (pointed below), and is breaking my real...
35
by: Bill Cunningham | last post by:
My string.h headers declares two functions I have been using called memfrob and strfry. They are encryption types functions. My man pages say they are standard to linux c and gnu c. They sure...
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: 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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.