⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 266.html

📁 Python Ebook Python&XML
💻 HTML
📖 第 1 页 / 共 2 页
字号:
					<Pre>
						
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */
if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyString_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;

					</pRe>

					<p>With the second function set, the need to call <tt ClasS="monofont">PyErr_NoMemory()</TT> is obviated:</P>

					<pre>
						
PyObject *res;
char *buf = (char *) Py_Malloc(BUFSIZ); /* for I/O */
if (buf == NULL)
    return NULL;
/* ...Do some I/O operation involving buf... */
res = PyString_FromString(buf);
Py_Free(buf); /* allocated with Py_Malloc */
return res;

					</pRE>

					<P>Here's the same code using the macro set:</P>

					<pre>
						
PyObject *res;
char *buf = PyMem_NEW(char, BUFSIZ); /* for I/O */
if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyString_FromString(buf);
PyMem_DEL(buf); /* allocated with PyMem_NEW */
return res;

					</pRE>

					<P>Note that in the three previous examples, the buffer is always manipulated via functions/macros belonging to the same set. Indeed, it is required to use the same memory API family for a given memory block so that the risk of mixing different allocators is reduced to a minimum. The following code sequence contains two errors, one of which is labeled as fatal because it mixes two different allocators operating on different heaps.</P>

					<pre>
						
char *buf1 = PyMem_NEW(char, BUFSIZ);
char *buf2 = (char *) malloc(BUFSIZ);
char *buf3 = (char *) PyMem_Malloc(BUFSIZ);
...
PyMem_DEL(buf3);  /* Wrong -- should be PyMem_Free() */
free(buf2);       /* Right -- allocated via malloc() */
free(buf1);       /* Fatal -- should be PyMem_DEL()  */

					</pRE>

					<P>In addition to the functions aimed at handling raw memory blocks from the Python heap, objects in Python are allocated and released with <Tt class="monofont">_PyObject_New()</tt> and <tt class="monofont">_PyObject_NewVar(),</tt> or with their corresponding macros <tt cLasS="monofont">PyObject_NEW()</tt> and <tT clasS="monofont">PyObject_NEW_VAR().</tt><a NAME="idx1073751308"></a><a naME="idx1073751309"></A><A name="idx1073751310"></A><A NAme="idx1073751311"></a><a NAME="idx1073751312"></a></p>

				
			</font>
<P><TABLE width="100%" border=0><TR valign="top"><TD><font size=1 color="#C0C0C0"><br></font></TD><TD align=right><font size=1 color="#C0C0C0">Last updated on 1/30/2002<br>Python Developer's Handbook, &copy;&nbsp;2002 Sams Publishing</font></TD></TR></TABLE></P>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="265.html" title="Initialization, Finalization, and Threads"><font size="1">&lt;&nbsp;BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0672319942&snode=266" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="266.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="267.html" title="Defining New Object Types"><font size="1">CONTINUE&nbsp;&gt;</font></a></td></TR></TABLE>
</TD></TR></TABLE>
<br><TABLE width=100% bgcolor=white border=0 cellspacing=0 cellpadding=5><TR><TD><H4 class=Title>Index terms contained in this section</H4>
<font size=2>
allocators<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073751294">raw memory</a><BR>
Application Programmers Interface (API)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Python/C<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073751299">managing memory</a> <a href="#idx1073751304">2nd</a> <a href="#idx1073751309">3rd</a><BR>
C library<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073751296">memory corruption</a><BR>
corruption<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073751295">memory</a><BR>
interfaces<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Python/C Application Programmers (API)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073751300">managing memory</a> <a href="#idx1073751305">2nd</a> <a href="#idx1073751310">3rd</a><BR>
library<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073751297">memory corruption</a><BR>
managing<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073751301">memory, Python/C Application Programmers Interface (API)</a> <a href="#idx1073751306">2nd</a> <a href="#idx1073751311">3rd</a><BR>
memory<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073751302">managing, Python/C Application Programmers Interface (API)</a> <a href="#idx1073751307">2nd</a> <a href="#idx1073751312">3rd</a><BR>
Python/C Application Programmers Interface (API)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073751298">managing memory</a> <a href="#idx1073751303">2nd</a> <a href="#idx1073751308">3rd</a><BR>
 <a href="#idx1073751293">raw memory allocators</a><BR>
<BR>
</font></TD></TR></TABLE>




<!--EndOfBrowse-->

</TD></TR></TABLE>
<table width=100% border=0 cellspacing=0 cellpadding=0 bgcolor=#990000><tr><td><p align=center><font size=1 face="verdana,arial,helvetica" color=white>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -