📄 free.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html><head><title>Memory Allocation Subsystem</title><style type="text/css">body { margin: auto; font-family: "Verdana" "sans-serif"; padding: 8px 1%;}a { color: #45735f }a:visited { color: #734559 }.logo { position:absolute; margin:3px; }.tagline { float:right; text-align:right; font-style:italic; width:240px; margin:12px; margin-top:58px;}.toolbar { font-variant: small-caps; text-align: center; line-height: 1.6em; margin: 0; padding:1px 8px;}.toolbar a { color: white; text-decoration: none; padding: 6px 12px; }.toolbar a:visited { color: white; }.toolbar a:hover { color: #80a796; background: white; }.content { margin: 5%; }.content dt { font-weight:bold; }.content dd { margin-bottom: 25px; margin-left:20%; }.content ul { padding:0px; padding-left: 15px; margin:0px; }/* rounded corners */.se { background: url(../images/se.png) 100% 100% no-repeat #80a796}.sw { background: url(../images/sw.png) 0% 100% no-repeat }.ne { background: url(../images/ne.png) 100% 0% no-repeat }.nw { background: url(../images/nw.png) 0% 0% no-repeat }</style><meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head><body><div><!-- container div to satisfy validator --><a href="../index.html"><img class="logo" src="../images/SQLite.gif" alt="SQLite Logo" border="0"></a><div><!-- IE hack to prevent disappearing logo--></div><div class="tagline">Small. Fast. Reliable.<br>Choose any three.</div><table width=100% style="clear:both"><tr><td> <div class="se"><div class="sw"><div class="ne"><div class="nw"> <div class="toolbar"> <a href="../about.html">About</a> <a href="../sitemap.html">Sitemap</a> <a href="../docs.html">Documentation</a> <a href="../download.html">Download</a> <a href="../copyright.html">License</a> <a href="../news.html">News</a> <a href="http://www.sqlite.org/cvstrac/index">Developers</a> <a href="../support.html">Support</a> </div></div></div></div></div></td></tr></table> <a href="intro.html"><h2>SQLite C Interface</h2></a><h2>Memory Allocation Subsystem</h2><blockquote><pre>void *sqlite3_malloc(int);void *sqlite3_realloc(void*, int);void sqlite3_free(void*);</pre></blockquote><p>The SQLite core uses these three routines for all of its owninternal memory allocation needs. "Core" in the previous sentencedoes not include operating-system specific VFS implementation. Thewindows VFS uses native malloc and free for some operations.</p><p>The sqlite3_malloc() routine returns a pointer to a blockof memory at least N bytes in length, where N is the parameter.If sqlite3_malloc() is unable to obtain sufficient freememory, it returns a NULL pointer. If the parameter N tosqlite3_malloc() is zero or negative then sqlite3_malloc() returnsa NULL pointer.</p><p>Calling sqlite3_free() with a pointer previously returnedby sqlite3_malloc() or sqlite3_realloc() releases that memory sothat it might be reused. The sqlite3_free() routine isa no-op if is called with a NULL pointer. Passing a NULL pointerto sqlite3_free() is harmless. After being freed, memoryshould neither be read nor written. Even reading previously freedmemory might result in a segmentation fault or other severe error.Memory corruption, a segmentation fault, or other severe errormight result if sqlite3_free() is called with a non-NULL pointer thatwas not obtained from sqlite3_malloc() or sqlite3_free().</p><p>The sqlite3_realloc() interface attempts to resize aprior memory allocation to be at least N bytes, where N is thesecond parameter. The memory allocation to be resized is the firstparameter. If the first parameter to sqlite3_realloc()is a NULL pointer then its behavior is identical to callingsqlite3_malloc(N) where N is the second parameter to sqlite3_realloc().If the second parameter to sqlite3_realloc() is zero ornegative then the behavior is exactly the same as callingsqlite3_free(P) where P is the first parameter to sqlite3_realloc().Sqlite3_realloc() returns a pointer to a memory allocationof at least N bytes in size or NULL if sufficient memory is unavailable.If M is the size of the prior allocation, then min(N,M) bytesof the prior allocation are copied into the beginning of buffer returnedby sqlite3_realloc() and the prior allocation is freed.If sqlite3_realloc() returns NULL, then the prior allocationis not freed.</p><p>The memory returned by sqlite3_malloc() and sqlite3_realloc()is always aligned to at least an 8 byte boundary.</p><p>The default implementationof the memory allocation subsystem uses the malloc(), realloc()and free() provided by the standard C library. However, ifSQLite is compiled with the following C preprocessor macro</p><p><blockquote> SQLITE_MEMORY_SIZE=<i>NNN</i> </blockquote></p><p>where <i>NNN</i> is an integer, then SQLite create a staticarray of at least <i>NNN</i> bytes in size and use that arrayfor all of its dynamic memory allocation needs. Additionalmemory allocator options may be added in future releases.</p><p>In SQLite version 3.5.0 and 3.5.1, it was possible to definethe SQLITE_OMIT_MEMORY_ALLOCATION which would cause the built-inimplementation of these routines to be omitted. That capabilityis no longer provided. Only built-in memory allocators can beused.</p><p>The windows OS interface layer callsthe system malloc() and free() directly when convertingfilenames between the UTF-8 encoding used by SQLiteand whatever filename encoding is used by the particular windowsinstallation. Memory allocation errors are detected, butthey are reported back as <a href="../c3ref/c_abort.html">SQLITE_CANTOPEN</a> or<a href="../c3ref/c_abort.html">SQLITE_IOERR</a> rather than <a href="../c3ref/c_abort.html">SQLITE_NOMEM</a>.</p><p><h3>Invariants:</h3><table border="0" cellpadding="5" cellspacing="0"><tr><td valign="top">F17303</td> <td valign="top">The <a href="../c3ref/free.html">sqlite3_malloc(N)</a> interface returns either a pointer tonewly checked-out block of at least N bytes of memorythat is 8-byte aligned,or it returns NULL if it is unable to fulfill the request.</td></tr><tr><td valign="top">F17304</td> <td valign="top">The <a href="../c3ref/free.html">sqlite3_malloc(N)</a> interface returns a NULL pointer ifN is less than or equal to zero.</td></tr><tr><td valign="top">F17305</td> <td valign="top">The <a href="../c3ref/free.html">sqlite3_free(P)</a> interface releases memory previouslyreturned from <a href="../c3ref/free.html">sqlite3_malloc()</a> or <a href="../c3ref/free.html">sqlite3_realloc()</a>,making it available for reuse.</td></tr><tr><td valign="top">F17306</td> <td valign="top">A call to <a href="../c3ref/free.html">sqlite3_free(NULL)</a> is a harmless no-op.</td></tr><tr><td valign="top">F17310</td> <td valign="top">A call to <a href="../c3ref/free.html">sqlite3_realloc(0,N)</a> is equivalent to a callto <a href="../c3ref/free.html">sqlite3_malloc(N)</a>.</td></tr><tr><td valign="top">F17312</td> <td valign="top">A call to <a href="../c3ref/free.html">sqlite3_realloc(P,0)</a> is equivalent to a callto <a href="../c3ref/free.html">sqlite3_free(P)</a>.</td></tr><tr><td valign="top">F17315</td> <td valign="top">The SQLite core uses <a href="../c3ref/free.html">sqlite3_malloc()</a>, <a href="../c3ref/free.html">sqlite3_realloc()</a>,and <a href="../c3ref/free.html">sqlite3_free()</a> for all of its memory allocation anddeallocation needs.</td></tr><tr><td valign="top">F17318</td> <td valign="top">The <a href="../c3ref/free.html">sqlite3_realloc(P,N)</a> interface returns either a pointerto a block of checked-out memory of at least N bytes in sizethat is 8-byte aligned, or a NULL pointer.</td></tr><tr><td valign="top">F17321</td> <td valign="top">When <a href="../c3ref/free.html">sqlite3_realloc(P,N)</a> returns a non-NULL pointer, it firstcopies the first K bytes of content from P into the newly allocatedwhere K is the lessor of N and the size of the buffer P.</td></tr><tr><td valign="top">F17322</td> <td valign="top">When <a href="../c3ref/free.html">sqlite3_realloc(P,N)</a> returns a non-NULL pointer, it firstreleases the buffer P.</td></tr><tr><td valign="top">F17323</td> <td valign="top">When <a href="../c3ref/free.html">sqlite3_realloc(P,N)</a> returns NULL, the buffer P isnot modified or released.</td></tr></table></p><p><h3>Limitations:</h3><table border="0" cellpadding="5" cellspacing="0"><tr><td valign="top">U17350</td> <td valign="top">The pointer arguments to <a href="../c3ref/free.html">sqlite3_free()</a> and <a href="../c3ref/free.html">sqlite3_realloc()</a>must be either NULL or else a pointer obtained from a priorinvocation of <a href="../c3ref/free.html">sqlite3_malloc()</a> or <a href="../c3ref/free.html">sqlite3_realloc()</a> that hasnot been released.</td></tr><tr><td valign="top">U17351</td> <td valign="top">The application must not read or write any part ofa block of memory after it has been released using<a href="../c3ref/free.html">sqlite3_free()</a> or <a href="../c3ref/free.html">sqlite3_realloc()</a>.</td></tr></table></p><p>See also lists of <a href="objlist.html">Objects</a>, <a href="constlist.html">Constants</a>, and <a href="funclist.html">Functions</a>.</p><hr><small><i>This page last modified 2008/05/12 13:08:44 UTC</i></small></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -