有关malloc,free等函数怎么用,给些例子!
问题1:今天看了链表,接触了malloc,calloc,free,realloc函数,但是书上没有多少例子,希望大家给些应用他们的很简单 的例子,最好有标注,新手。
问题2:下面这个程序什么意思,那个(!p)是什么意思!最好也具体解释一下这个程序,谢谢啦!
#include<stdlib.h>
#include<stdio.h>
void main()
{
int *p,t;
p=(int *)malloc(40*sizeof(int));
if(!p)
{printf("\t内存已用完\t");
exit(0);
}
for(t=0;t<40;++t) *(p+t)=t;
for(t=0;t<40;++t) printf("\t%d",*(p+t));
free(p);
}
问题3:我上次见有些人给出源代码排版得很工整,怎么弄,
[解决办法]
!p 等价于 p != 0
[解决办法]
排版的话,用上面那排按钮中的“插入源代码”的方式
[解决办法]
void main() { int *p,t; //指针p申请一个40个int大小的空间 可以看成p指向int arr[40]的数组 p=(int *)malloc(40*sizeof(int)); //p为空 才会执行里面的打印 没申请到空间 if(!p) { printf("\t内存已用完\t"); exit(0); } //利用p对这块赋值 也可以写成p[t]=t; for(t=0;t<40;++t) *(p+t)=t; //打印 for(t=0;t<40;++t) printf("\t%d",*(p+t)); //释放p指向的空间 free(p); }
[解决办法]
#include<stdlib.h>#include<stdio.h>int main(){ int *p,t; p=(int *)malloc(40*sizeof(int)); if(!p){ printf("\t内存已用完\t\n"); exit(0); } for(t=0;t<40;++t) *(p+t)=t; for(t=0;t<40;++t) printf("\t%d",*(p+t)); free(p); p = NULL; return 0;}//我只会排版,哈哈
[解决办法]
回答第3个问题:编辑帖子时有个插入源代码的控件,这样写的代码就排版的很好了。
[解决办法]
lz该多看几遍书了。
买本c primer看看
[解决办法]
!p == p != NULL
[解决办法]
//将c:\\tmp文件夹下的所有文件的内容全部放到用malloc分配的内存中#include <stdio.h>#include <stdlib.h>#include <string.h>#include <io.h>struct FB { char fn[256]; size_t fl; char *b; struct FB *next; struct FB *prev;} *fh,*fb,*ft;char ln[256];char fpn[256];FILE *af;FILE *f;int L,n;int main() { system("dir /b /a-d c:\\tmp\\*.* >c:\\allfn.txt"); af=fopen("c:\\allfn.txt","r"); if (NULL==af) { printf("Can not open file c:\\allfn.txt!\n"); return 1; } fh=NULL; fb=NULL; n=0; while (1) { if (NULL==fgets(ln,256,af)) break; L=strlen(ln); if ('\n'==ln[L-1]) ln[L-1]=0; printf("read %s\n",ln); strcpy(fpn,"c:\\tmp\\"); strcat(fpn,ln); ft=(struct FB *)malloc(sizeof(struct FB)); if (NULL==ft) { printf("Can not malloc ft!\n"); fclose(af); return 2;//之前的malloc在main退出后由操作系统自动free } printf("ft[%d]==%p\n",n,ft); strcpy(ft->fn,fpn); f=fopen(fpn,"rb"); if (NULL==f) { printf("Can not open file %s!\n",fpn); fclose(af); return 3;//之前的malloc在main退出后由操作系统自动free } ft->fl=_filelength(fileno(f)); ft->b=malloc(ft->fl); if (NULL==ft->b) { printf("Can not malloc ft->b!\n"); fclose(f); fclose(af); return 4;//之前的malloc在main退出后由操作系统自动free } printf("ft[%d]->b==%p\n",n,ft->b); if (ft->fl!=fread(ft->b,1,ft->fl,f)) { printf("fread error!\n"); fclose(f); fclose(af); return 5;//之前的malloc在main退出后由操作系统自动free } fclose(f); ft->next=NULL; if (NULL==fh) { ft->prev=NULL; fh=ft; } else { fb->next=ft; ft->prev=fb; } fb=ft; n++; } fclose(af); printf("-----list-----\n"); for (ft=fh;NULL!=ft;ft=ft->next) { printf("%8d %s\n",ft->fl,ft->fn); if (NULL!=ft) fb=ft; } printf("-----free-----\n"); n--; if (NULL!=fh) { for (ft=fb->prev;NULL!=ft;ft=ft->prev) { if (NULL!=ft->next->b) { printf("ft[%d]->b==%p\n",n,ft->next->b); free(ft->next->b); } if (NULL!=ft->next) { printf("ft[%d]==%p\n",n,ft->next); free(ft->next); } n--; } if (NULL!=fh->b) { printf("ft[0]->b==%p\n",fh->b); free(fh->b); } printf("ft[0]==%p\n",fh); free(fh); } return 0;}//C:\tmp\tmp\Debug>dir /a-d c:\tmp// 驱动器 C 中的卷是 C_HD5_1// 卷的序列号是 1817-D526//// c:\tmp 的目录////找不到文件////C:\tmp\tmp\Debug>tmp//找不到文件//-----list-----//-----free-----////C:\tmp\tmp\Debug>dir /a-d c:\tmp// 驱动器 C 中的卷是 C_HD5_1// 卷的序列号是 1817-D526//// c:\tmp 的目录////2011-06-30 18:04 44,840 my_c.rar//2011-06-30 17:18 1,036 err.frm//2011-06-30 14:32 14,243 出租.txt//2011-06-28 12:08 23,681 MSDN98书签.txt// 4 个文件 83,800 字节// 0 个目录 17,041,870,848 可用字节////C:\tmp\tmp\Debug>tmp//read my_c.rar//ft[0]==00421800//ft[0]->b==00520068//read err.frm//ft[1]==00421670//ft[1]->b==0052AFC0//read 出租.txt//ft[2]==00421530//ft[2]->b==00378F28//read MSDN98书签.txt//ft[3]==004213F0//ft[3]->b==0052B3F8//-----list-----// 44840 c:\tmp\my_c.rar// 1036 c:\tmp\err.frm// 14243 c:\tmp\出租.txt// 23681 c:\tmp\MSDN98书签.txt//-----free-----//ft[3]->b==0052B3F8//ft[3]==004213F0//ft[2]->b==00378F28//ft[2]==00421530//ft[1]->b==0052AFC0//ft[1]==00421670//ft[0]->b==00520068//ft[0]==00421800////C:\tmp\tmp\Debug>
[解决办法]
1,关于malloc和free的小例子
#include "stdio.h"#include "stdlib.h"int main(){ char *pszText = NULL; //定义一个指向字符串的指针变量,并初始化为空 pszText = (char *)malloc(1024); //申请1024个字节的内存 if(NULL != pszText) //不为空表示分配成功 { memset(pszText, 0, 1024); //初始化该内存,malloc申请的内存不会自动初始化 sprintf(pszText, "Hello World!"); //将"Hello World!"打印到该内存中 printf("%s\n", pszText); //以字符串的格式打印该内存的值至控制台 free(pszText); //释放该内存 } else //分配失败 { printf("内存分配失败!\n"); } return 0;}