来自bccn C语言论坛
首先声明一点,本文为转贴。
浅谈C中的malloc和free
作者:lj_860603 阅读人次:43013 文章来源:本站原创 发布时间:2006-8-5 网友评论(32)条
原帖及讨论:http://bbs.bccn.net/thread-82212-1-1.html
在C语言的学习中,对内存管理这部分的知识掌握尤其重要!之前对C中的malloc()和free()两个函数的了解甚少,只知道大概该怎么用——就是malloc然后free就一切OK了。当然现在对这两个函数的体会也不见得多,不过对于本文章第三部分的内容倒是有了转折性的认识,所以
写下这篇文章作为一个对知识的总结。这篇文章之所以命名中有个“浅谈”的字眼,也就是这个意思了!希望对大家有一点帮助!
如果不扯得太远的话(比如说操作系统中虚拟内存和物理内存如何运做如何管理之类的知识等),我感觉这篇文章应该是比较全面地谈了一下malloc()和free().这篇文章由浅入深(不见得有多深)分三个部分介绍主要内容。
废话了那么多,下面立刻进入主题================》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》� ......
下面说到的C语言中的知识,我只是在工作中经常见到或用到,但从未深究为什么,今天才却道原来简简单单下面孕育这无穷的知识点和我的盲点,是该边学习边记录了。
预处理器(Preprocessor)
1 . 用预处理指令#define 声明一个常数,用以表明1年中有多少秒(忽略闰年问题)
#define SECONDS_PER_YEAR (60 * 60 * 24 * 365)UL
我在这想看到几件事情:
1) #define 语法的基本知识(例如:不能以分号结束,括号的使用,等等)
2)懂得预处理器将为你计算常数表达式的值,因此,直接写出你是如何计算一年中有多少秒而不是计算出实际的值,是更清晰而没有代价的。
3) 意识到这个表达式将使一个16位机的整型数溢出-因此要用到长整型符号L,告诉编译器这个常数是的长整型数。
4) 如果你在你的表达式中用到UL(表示无符号长整型),那么你有了一个好的起点。记住,第一印象很重要。
2 . 写一个"标准"宏MIN ,这个宏输入两个参数并返回较小的一个。
#define MIN(A,B) ((A) <= (B) ? (A) : (B))
这个测试是为下面的目的而设的:
1) 标识#define在宏中应用的基本知识。这是很重要的。因为在 ......
; 编译链接方法
; (ld 的‘-s’选项意为“strip all”)
; gcc -c not link
;
; [root@XXX XXX]# nasm -f elf foo.asm -o foo.o
; [root@XXX XXX]# gcc -c bar.c -o bar.o
; [root@XXX XXX]# ld -s foo.o bar.o -o foobar
; [root@XXX XXX]# ./foobar
; the 2nd one
; [root@XXX XXX]#
extern choose ; int choose(int a, int b);
[section .data] ; 数据在此
num1st dd 3
num2nd dd 4
[section .text] ; 代码在此
global _start ; 我们必须导出 _start 这个入口,以便让链接器识别。
global myprint ; 导出这个函数为了让 bar.c 使用
_start:
push num2nd ; ┓
push num1st ; ┃
call choose ; ┣ choose(num1st, num2nd);
add esp, 4 ; ┛
mov ebx, 0
mov eax, 1 ; sys_exit
int 0x80 ; 系统调用
; void myprint(char* msg, int len)
myprint:
mov edx, [esp + 8] ; len
mov ecx, [esp + 4] ; msg
mov ebx, 1
mov eax, 4 ; sys_write
int 0x80 ; 系统调用
ret
void myprint(char* msg, int len);
int choose(int a, int b)
{
if(a >= b){
myprint("the 1st one\n", 13);
}
......
<%
'以下是试用版代码,是固定的代码,只有正式版才可自动从后台数据库中读取图片。
piao1="images/1.jpg"
piao1url=""
piaos1="图1"
piao2="images/2.jpg"
piao2url=""
piaos2="图2"
piao3="images/3.jpg"
piao3url=""
piaos3="图3"
piao4="images/4.jpg"
piao4url=""
piaos4="图4"
piao5="images/5.jpg"
piao5url=""
piaos5="图5"
pic=piao1&"|"&piao2&"|"&piao3&"|"&piao4&"|"&piao5
link=piao1url&"|"&piao2url&"|"&piao3url&"|"&piao4url&"|"&piao5url
texts=piaos1&"|"&piaos2&"|"&piaos3&"|"&piaos4&"|"&piaos5
%>
<script language="" type="text/javascript">
<!--
......
<%
Const New_img=10
set rs_Product=server.createobject("adodb.recordset")
sqltext="select top " & New_img & " * from 0791idc_Product order by ID"
rs_Product.open sqltext,conn,1,1
if not rs_Product.EOF then%>
<div align='center' id='demo' style='overflow:hidden;height:200px;width:700px;'><!--滚动区的高度和宽度-->
<table align='center' cellpadding='0' cellspace='0' border='0'>
<tr>
<td id='demo1' valign='top'>
<table width='100%' cellpadding='0' cellspacing='0' border='0' align='center'>
<tr valign='top'>
<%
while not rs_Product.EOF
fileExt=lcase(getFileExtName(rs_Product("DefaultPicUrl")))
%>
<td align='center'>
<TABLE width=100% border=0 align=center cellPadding=0 cellSpacing=0>
<TR>
<TD align="c ......
<%
Set rs=Server.CreateObject("ADODB.RecordSet")
sql="select * from 0791idc_News where BigClassName='家装课堂' order by ID desc"
rs.Open sql,Conn,1,1
if rs.eof and rs.bof then
response.Write("暂时没有记录")
else
%>
<%
rs.PageSize=15
for j=1 to rs.PageSize
ViewList=3
%>
<td width="33%">
<table width="100%" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td width="5%" height="27"> </td>
<td width="5%" valign="middle" style="border-bottom:1px dotted #9b7022;"><img src="images/a. ......