Commit 9ff53d42 authored by Andre Maroneze's avatar Andre Maroneze 💬
Browse files

blog: add some missing posts and rename some urls

parent 3ba4200e
Pipeline #31357 passed with stages
in 3 minutes and 55 seconds
---
layout: post
author: Pascal Cuoq
date: 2011-08-26 09:19 +0200
categories:
format: xhtml
title: "Think of a number, any number"
summary:
---
{% raw %}
<p>I like to think the sentence this post borrows as title is one of the most condensed jokes in The Hitch-Hiker's Guide to the Galaxy series, if one takes the sentence to mean "Pick any number using an uniform probability". It may seem that I am over-interpreting, and perhaps I am, but a similar "division by zero" joke is made explicit elsewhere in the series.</p>
{% endraw %}
---
layout: post
author: Pascal Cuoq
date: 2012-02-09 14:28 +0200
categories:
format: xhtml
title: "Function realloc() is broken - Not"
summary:
---
{% raw %}
<p>This post is a sequel of <a href="/2012/01/05/Double-free-no-such-thing.html">this post</a>, in which I argued that it is not possible to double-free a piece of memory, only to pass indeterminate data (specifically, a dangling pointer) to a function (specifically, <code>free()</code>).</p>
<h2>Broken</h2>
<p>This time I am arguing that the standardized function <code>realloc()</code> is broken. By this I do not mean that a particular implementation is broken. This happens, as I found out searching the web for these very words, just before starting on this post. I mean that the standard definition is broken: it forces the programmer to write eir program either in such a way that it may manipulate indeterminate data (specifically, a dangling pointer) or in such a way that it may leak memory (specifically, the block that couldn't be enlarged).</p>
<h2>Naively using <code>realloc()</code>: memory leak</h2>
<pre>char *p = malloc(...);
...
p = realloc(p, ...);
if (p)
{
/* good: we got the additional
memory we need. */
...
}
else
{
/* We didn't get the memory. Ah well...
We didn't need it that badly anyway,
let's continue. */
...
}
</pre>
<p>The problem with this program is that it leaks memory. In the <code>else</code> branch, the block has been preciously kept in memory by <code>realloc()</code>, but the variable <code>p</code> has been overwritten with <code>NULL</code>, the result of the <code>realloc()</code> call, so that there is no way to access it any more or, for that matter, to free it.</p>
<h2>The idiomatic solution</h2>
<p>The above is a standard pitfall of <code>realloc()</code>, and there is an idiomatic solution:</p>
<pre>char *p = malloc(...);
...
char *old = p;
p = realloc(p, ...);
if (p)
{
/* good: we got the additional
memory we need. */
...
}
else
{
/* We didn't get the memory. Ah well...
We didn't need it that badly anyway.
In fact, let's throw away the block right now! */
free(old);
...
}
</pre>
{% endraw %}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment