382 lines
17 KiB
HTML
382 lines
17 KiB
HTML
<html>
|
||
<head>
|
||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
||
<title>Chapter 6. Memory (de)allocation</title>
|
||
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
|
||
<link rel="home" href="index.html" title="igraph Reference Manual">
|
||
<link rel="up" href="index.html" title="igraph Reference Manual">
|
||
<link rel="prev" href="igraph-Error.html" title="Chapter 5. Error handling">
|
||
<link rel="next" href="igraph-Data-structures.html" title="Chapter 7. Data structure library: vector, matrix, other data types">
|
||
<script type="text/javascript" src="toggle.js"></script><link rel="stylesheet" href="style.css" type="text/css">
|
||
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" type="text/css">
|
||
<link rel="chapter" href="igraph-Introduction.html" title="Chapter 1. Introduction">
|
||
<link rel="chapter" href="igraph-Installation.html" title="Chapter 2. Installation">
|
||
<link rel="chapter" href="igraph-Tutorial.html" title="Chapter 3. Tutorial">
|
||
<link rel="chapter" href="igraph-Basic.html" title="Chapter 4. Basic data types and interface">
|
||
<link rel="chapter" href="igraph-Error.html" title="Chapter 5. Error handling">
|
||
<link rel="chapter" href="igraph-Memory.html" title="Chapter 6. Memory (de)allocation">
|
||
<link rel="chapter" href="igraph-Data-structures.html" title="Chapter 7. Data structure library: vector, matrix, other data types">
|
||
<link rel="chapter" href="igraph-Random.html" title="Chapter 8. Random numbers">
|
||
<link rel="chapter" href="igraph-Iterators.html" title="Chapter 9. Vertex and edge selectors and sequences, iterators">
|
||
<link rel="chapter" href="igraph-Attributes.html" title="Chapter 10. Graph, vertex and edge attributes">
|
||
<link rel="chapter" href="igraph-Generators.html" title="Chapter 11. Deterministic graph generators">
|
||
<link rel="chapter" href="igraph-Games.html" title='Chapter 12. Stochastic graph generators ("games")'>
|
||
<link rel="chapter" href="igraph-Bipartite.html" title="Chapter 13. Bipartite, i.e. two-mode graphs">
|
||
<link rel="chapter" href="igraph-Spatial.html" title="Chapter 14. Spatial graphs">
|
||
<link rel="chapter" href="igraph-Operators.html" title="Chapter 15. Graph operators">
|
||
<link rel="chapter" href="igraph-Visitors.html" title="Chapter 16. Graph visitors">
|
||
<link rel="chapter" href="igraph-Structural.html" title="Chapter 17. Structural properties of graphs">
|
||
<link rel="chapter" href="igraph-Cycles.html" title="Chapter 18. Graph cycles">
|
||
<link rel="chapter" href="igraph-Cliques.html" title="Chapter 19. Cliques and independent vertex sets">
|
||
<link rel="chapter" href="igraph-Motifs.html" title="Chapter 20. Graph motifs, dyad census and triad census">
|
||
<link rel="chapter" href="igraph-Isomorphism.html" title="Chapter 21. Graph isomorphism">
|
||
<link rel="chapter" href="igraph-Coloring.html" title="Chapter 22. Graph coloring">
|
||
<link rel="chapter" href="igraph-Flows.html" title="Chapter 23. Maximum flows, minimum cuts and related measures">
|
||
<link rel="chapter" href="igraph-Separators.html" title="Chapter 24. Vertex separators">
|
||
<link rel="chapter" href="igraph-Community.html" title="Chapter 25. Detecting community structure">
|
||
<link rel="chapter" href="igraph-Graphlets.html" title="Chapter 26. Graphlets">
|
||
<link rel="chapter" href="igraph-HRG.html" title="Chapter 27. Hierarchical random graphs">
|
||
<link rel="chapter" href="igraph-Embedding.html" title="Chapter 28. Embedding of graphs">
|
||
<link rel="chapter" href="igraph-Layout.html" title="Chapter 29. Generating layouts for graph drawing">
|
||
<link rel="chapter" href="igraph-Processes.html" title="Chapter 30. Processes on graphs">
|
||
<link rel="chapter" href="igraph-Foreign.html" title="Chapter 31. Reading and writing graphs from and to files">
|
||
<link rel="chapter" href="igraph-Linalg.html" title="Chapter 32. Using BLAS, LAPACK and ARPACK for igraph matrices and graphs">
|
||
<link rel="chapter" href="igraph-Nongraph.html" title="Chapter 33. Non-graph related functions">
|
||
<link rel="chapter" href="igraph-Advanced.html" title="Chapter 34. Advanced igraph programming">
|
||
<link rel="chapter" href="igraph-Glossary.html" title="Chapter 35. Glossary">
|
||
<link rel="chapter" href="igraph-Licenses.html" title="Chapter 36. Licenses for igraph and this manual">
|
||
<link rel="index" href="ix01.html" title="Index">
|
||
</head>
|
||
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
|
||
<div class="navigation-header mb-4" width="100%" summary="Navigation header"><div class="btn-group">
|
||
<a accesskey="p" class="btn btn-light" href="igraph-Error.html"><i class="fa fa-chevron-left"></i>
|
||
Previous
|
||
</a><a accesskey="h" class="btn btn-light" href="index.html"><i class="fa fa-home"></i>
|
||
Home
|
||
</a><a accesskey="n" class="btn btn-light" href="igraph-Data-structures.html"><i class="fa fa-chevron-right"></i>
|
||
Next
|
||
</a>
|
||
</div></div>
|
||
<div class="chapter">
|
||
<div class="titlepage"><div><div><h1 class="title">
|
||
<a name="igraph-Memory"></a>Chapter 6. Memory (de)allocation</h1></div></div></div>
|
||
<div class="toc"><dl class="toc">
|
||
<dt><span class="section"><a href="igraph-Memory.html#about-alloc-funcs">1. About allocation functions</a></span></dt>
|
||
<dt><span class="section"><a href="igraph-Memory.html#available-alloc-funcs">2. Available allocation functions</a></span></dt>
|
||
</dl></div>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
|
||
<a name="about-alloc-funcs"></a>1. About allocation functions</h2></div></div></div>
|
||
<p>
|
||
Some igraph functions return a pointer vector (<span class="type">igraph_vector_ptr_t</span>)
|
||
containing pointers to other igraph or other data types. These data
|
||
types are dynamically allocated and have to be deallocated
|
||
manually when the user does not need them any more. <span class="type">igraph_vector_ptr_t</span>
|
||
has functions to deallocate the contained pointers on its own, but in this
|
||
case it has to be ensured that these pointers are allocated by a function
|
||
that corresponds to the deallocator function that igraph uses.
|
||
</p>
|
||
<p>
|
||
To this end, igraph exports the memory allocation functions that are used
|
||
internally so the user of the library can ensure that the proper functions
|
||
are used when pointers are moved between the code written by the user and
|
||
the code of the igraph library.
|
||
</p>
|
||
<p>
|
||
Additionally, the memory allocator functions used by igraph work around the
|
||
quirks of classical <code class="constant">malloc</code>(), <code class="constant">realloc</code>() and <code class="constant">calloc</code>() implementations
|
||
where the behaviour of allocating zero bytes is undefined. igraph allocator
|
||
functions will always allocate at least one byte.
|
||
</p>
|
||
</div>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
|
||
<a name="available-alloc-funcs"></a>2. Available allocation functions</h2></div></div></div>
|
||
<div class="toc"><dl class="toc">
|
||
<dt><span class="section"><a href="igraph-Memory.html#igraph_malloc">2.1. <code class="function">igraph_malloc</code> — Allocates memory that can be safely deallocated by igraph functions.</a></span></dt>
|
||
<dt><span class="section"><a href="igraph-Memory.html#igraph_calloc">2.2. <code class="function">igraph_calloc</code> — Allocates memory that can be safely deallocated by igraph functions.</a></span></dt>
|
||
<dt><span class="section"><a href="igraph-Memory.html#igraph_realloc">2.3. <code class="function">igraph_realloc</code> — Reallocate memory that can be safely deallocated by igraph functions.</a></span></dt>
|
||
<dt><span class="section"><a href="igraph-Memory.html#igraph_free">2.4. <code class="function">igraph_free</code> — Deallocates memory that was allocated by igraph functions.</a></span></dt>
|
||
</dl></div>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h3 class="title">
|
||
<a name="igraph_malloc"></a>2.1. <code class="function">igraph_malloc</code> — Allocates memory that can be safely deallocated by igraph functions.</h3></div></div></div>
|
||
<a class="indexterm" name="id-1.7.3.2.2"></a><p>
|
||
</p>
|
||
<div class="informalexample"><pre class="programlisting">
|
||
void *igraph_malloc(size_t size);
|
||
</pre></div>
|
||
<p>
|
||
</p>
|
||
<p>
|
||
|
||
|
||
|
||
This function behaves like <code class="constant">malloc</code>(), but it ensures that at least one
|
||
byte is allocated even when the caller asks for zero bytes.
|
||
|
||
</p>
|
||
<p><b>Arguments: </b>
|
||
</p>
|
||
<div class="variablelist"><table border="0" class="variablelist">
|
||
<colgroup>
|
||
<col align="left" valign="top">
|
||
<col>
|
||
</colgroup>
|
||
<tbody><tr>
|
||
<td><p><span class="term"><em class="parameter"><code>size</code></em>:</span></p></td>
|
||
<td><p>
|
||
Number of bytes to be allocated. Zero is treated as one byte.
|
||
</p></td>
|
||
</tr></tbody>
|
||
</table></div>
|
||
<p>
|
||
</p>
|
||
<p><b>Returns: </b></p>
|
||
<div class="variablelist"><table border="0" class="variablelist">
|
||
<colgroup>
|
||
<col align="left" valign="top">
|
||
<col>
|
||
</colgroup>
|
||
<tbody><tr>
|
||
<td><p><span class="term"><em class="parameter"><code></code></em></span></p></td>
|
||
<td><p>
|
||
Pointer to the piece of allocated memory; <code class="constant">NULL</code> if the allocation
|
||
failed.
|
||
</p></td>
|
||
</tr></tbody>
|
||
</table></div>
|
||
<p>
|
||
|
||
</p>
|
||
<p><b>See also: </b></p>
|
||
<div class="variablelist"><table border="0" class="variablelist">
|
||
<colgroup>
|
||
<col align="left" valign="top">
|
||
<col>
|
||
</colgroup>
|
||
<tbody><tr>
|
||
<td><p><span class="term"><em class="parameter"><code></code></em></span></p></td>
|
||
<td><p>
|
||
<a class="link" href="igraph-Memory.html#igraph_calloc" title="2.2. igraph_calloc — Allocates memory that can be safely deallocated by igraph functions."><code class="function">igraph_calloc()</code></a>, <a class="link" href="igraph-Memory.html#igraph_realloc" title="2.3. igraph_realloc — Reallocate memory that can be safely deallocated by igraph functions."><code class="function">igraph_realloc()</code></a>, <a class="link" href="igraph-Memory.html#igraph_free" title="2.4. igraph_free — Deallocates memory that was allocated by igraph functions."><code class="function">igraph_free()</code></a>
|
||
</p></td>
|
||
</tr></tbody>
|
||
</table></div>
|
||
<p>
|
||
|
||
</p>
|
||
</div>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h3 class="title">
|
||
<a name="igraph_calloc"></a>2.2. <code class="function">igraph_calloc</code> — Allocates memory that can be safely deallocated by igraph functions.</h3></div></div></div>
|
||
<a class="indexterm" name="id-1.7.3.3.2"></a><p>
|
||
</p>
|
||
<div class="informalexample"><pre class="programlisting">
|
||
void *igraph_calloc(size_t count, size_t size);
|
||
</pre></div>
|
||
<p>
|
||
</p>
|
||
<p>
|
||
|
||
|
||
|
||
This function behaves like <code class="constant">calloc</code>(), but it ensures that at least one
|
||
byte is allocated even when the caller asks for zero bytes.
|
||
|
||
</p>
|
||
<p><b>Arguments: </b>
|
||
</p>
|
||
<div class="variablelist"><table border="0" class="variablelist">
|
||
<colgroup>
|
||
<col align="left" valign="top">
|
||
<col>
|
||
</colgroup>
|
||
<tbody>
|
||
<tr>
|
||
<td><p><span class="term"><em class="parameter"><code>count</code></em>:</span></p></td>
|
||
<td><p>
|
||
Number of items to be allocated.
|
||
</p></td>
|
||
</tr>
|
||
<tr>
|
||
<td><p><span class="term"><em class="parameter"><code>size</code></em>:</span></p></td>
|
||
<td><p>
|
||
Size of a single item to be allocated.
|
||
</p></td>
|
||
</tr>
|
||
</tbody>
|
||
</table></div>
|
||
<p>
|
||
</p>
|
||
<p><b>Returns: </b></p>
|
||
<div class="variablelist"><table border="0" class="variablelist">
|
||
<colgroup>
|
||
<col align="left" valign="top">
|
||
<col>
|
||
</colgroup>
|
||
<tbody><tr>
|
||
<td><p><span class="term"><em class="parameter"><code></code></em></span></p></td>
|
||
<td><p>
|
||
Pointer to the piece of allocated memory; <code class="constant">NULL</code> if the allocation
|
||
failed.
|
||
</p></td>
|
||
</tr></tbody>
|
||
</table></div>
|
||
<p>
|
||
|
||
</p>
|
||
<p><b>See also: </b></p>
|
||
<div class="variablelist"><table border="0" class="variablelist">
|
||
<colgroup>
|
||
<col align="left" valign="top">
|
||
<col>
|
||
</colgroup>
|
||
<tbody><tr>
|
||
<td><p><span class="term"><em class="parameter"><code></code></em></span></p></td>
|
||
<td><p>
|
||
<a class="link" href="igraph-Memory.html#igraph_malloc" title="2.1. igraph_malloc — Allocates memory that can be safely deallocated by igraph functions."><code class="function">igraph_malloc()</code></a>, <a class="link" href="igraph-Memory.html#igraph_realloc" title="2.3. igraph_realloc — Reallocate memory that can be safely deallocated by igraph functions."><code class="function">igraph_realloc()</code></a>, <a class="link" href="igraph-Memory.html#igraph_free" title="2.4. igraph_free — Deallocates memory that was allocated by igraph functions."><code class="function">igraph_free()</code></a>
|
||
</p></td>
|
||
</tr></tbody>
|
||
</table></div>
|
||
<p>
|
||
|
||
</p>
|
||
</div>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h3 class="title">
|
||
<a name="igraph_realloc"></a>2.3. <code class="function">igraph_realloc</code> — Reallocate memory that can be safely deallocated by igraph functions.</h3></div></div></div>
|
||
<a class="indexterm" name="id-1.7.3.4.2"></a><p>
|
||
</p>
|
||
<div class="informalexample"><pre class="programlisting">
|
||
void *igraph_realloc(void *ptr, size_t size);
|
||
</pre></div>
|
||
<p>
|
||
</p>
|
||
<p>
|
||
|
||
|
||
|
||
This function behaves like <code class="constant">realloc</code>(), but it ensures that at least one
|
||
byte is allocated even when the caller asks for zero bytes.
|
||
|
||
</p>
|
||
<p><b>Arguments: </b>
|
||
</p>
|
||
<div class="variablelist"><table border="0" class="variablelist">
|
||
<colgroup>
|
||
<col align="left" valign="top">
|
||
<col>
|
||
</colgroup>
|
||
<tbody>
|
||
<tr>
|
||
<td><p><span class="term"><em class="parameter"><code>ptr</code></em>:</span></p></td>
|
||
<td><p>
|
||
The pointer to reallocate.
|
||
</p></td>
|
||
</tr>
|
||
<tr>
|
||
<td><p><span class="term"><em class="parameter"><code>size</code></em>:</span></p></td>
|
||
<td><p>
|
||
Number of bytes to be allocated.
|
||
</p></td>
|
||
</tr>
|
||
</tbody>
|
||
</table></div>
|
||
<p>
|
||
</p>
|
||
<p><b>Returns: </b></p>
|
||
<div class="variablelist"><table border="0" class="variablelist">
|
||
<colgroup>
|
||
<col align="left" valign="top">
|
||
<col>
|
||
</colgroup>
|
||
<tbody><tr>
|
||
<td><p><span class="term"><em class="parameter"><code></code></em></span></p></td>
|
||
<td><p>
|
||
Pointer to the piece of allocated memory; <code class="constant">NULL</code> if the allocation
|
||
failed.
|
||
</p></td>
|
||
</tr></tbody>
|
||
</table></div>
|
||
<p>
|
||
|
||
</p>
|
||
<p><b>See also: </b></p>
|
||
<div class="variablelist"><table border="0" class="variablelist">
|
||
<colgroup>
|
||
<col align="left" valign="top">
|
||
<col>
|
||
</colgroup>
|
||
<tbody><tr>
|
||
<td><p><span class="term"><em class="parameter"><code></code></em></span></p></td>
|
||
<td><p>
|
||
<a class="link" href="igraph-Memory.html#igraph_free" title="2.4. igraph_free — Deallocates memory that was allocated by igraph functions."><code class="function">igraph_free()</code></a>, <a class="link" href="igraph-Memory.html#igraph_malloc" title="2.1. igraph_malloc — Allocates memory that can be safely deallocated by igraph functions."><code class="function">igraph_malloc()</code></a>
|
||
</p></td>
|
||
</tr></tbody>
|
||
</table></div>
|
||
<p>
|
||
|
||
</p>
|
||
</div>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h3 class="title">
|
||
<a name="igraph_free"></a>2.4. <code class="function">igraph_free</code> — Deallocates memory that was allocated by igraph functions.</h3></div></div></div>
|
||
<a class="indexterm" name="id-1.7.3.5.2"></a><p>
|
||
</p>
|
||
<div class="informalexample"><pre class="programlisting">
|
||
void igraph_free(void *ptr);
|
||
</pre></div>
|
||
<p>
|
||
</p>
|
||
<p>
|
||
|
||
|
||
|
||
This function exposes the <code class="constant">free</code>() function used internally by igraph.
|
||
|
||
</p>
|
||
<p><b>Arguments: </b>
|
||
</p>
|
||
<div class="variablelist"><table border="0" class="variablelist">
|
||
<colgroup>
|
||
<col align="left" valign="top">
|
||
<col>
|
||
</colgroup>
|
||
<tbody><tr>
|
||
<td><p><span class="term"><em class="parameter"><code>ptr</code></em>:</span></p></td>
|
||
<td><p>
|
||
Pointer to the piece of memory to be deallocated.</p></td>
|
||
</tr></tbody>
|
||
</table></div>
|
||
<p>
|
||
|
||
|
||
Time complexity: platform dependent, ideally it should be O(1).
|
||
|
||
</p>
|
||
<p><b>See also: </b></p>
|
||
<div class="variablelist"><table border="0" class="variablelist">
|
||
<colgroup>
|
||
<col align="left" valign="top">
|
||
<col>
|
||
</colgroup>
|
||
<tbody><tr>
|
||
<td><p><span class="term"><em class="parameter"><code></code></em></span></p></td>
|
||
<td><p>
|
||
<a class="link" href="igraph-Memory.html#igraph_calloc" title="2.2. igraph_calloc — Allocates memory that can be safely deallocated by igraph functions."><code class="function">igraph_calloc()</code></a>, <a class="link" href="igraph-Memory.html#igraph_malloc" title="2.1. igraph_malloc — Allocates memory that can be safely deallocated by igraph functions."><code class="function">igraph_malloc()</code></a>, <a class="link" href="igraph-Memory.html#igraph_realloc" title="2.3. igraph_realloc — Reallocate memory that can be safely deallocated by igraph functions."><code class="function">igraph_realloc()</code></a>
|
||
</p></td>
|
||
</tr></tbody>
|
||
</table></div>
|
||
<p>
|
||
|
||
</p>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<table class="navigation-footer" width="100%" summary="Navigation footer" cellpadding="2" cellspacing="0"><tr valign="middle">
|
||
<td align="left"><a accesskey="p" href="igraph-Error.html"><b>← Chapter 5. Error handling</b></a></td>
|
||
<td align="right"><a accesskey="n" href="igraph-Data-structures.html"><b>Chapter 7. Data structure library: vector, matrix, other data types →</b></a></td>
|
||
</tr></table>
|
||
</body>
|
||
</html>
|