惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
Microsoft Security Blog
Microsoft Security Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
V
Visual Studio Blog
宝玉的分享
宝玉的分享
IT之家
IT之家
人人都是产品经理
人人都是产品经理
T
The Blog of Author Tim Ferriss
I
InfoQ
B
Blog RSS Feed
T
Threatpost
博客园_首页
M
MIT News - Artificial intelligence
Spread Privacy
Spread Privacy
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Know Your Adversary
Know Your Adversary
U
Unit 42
Engineering at Meta
Engineering at Meta
C
Cyber Attacks, Cyber Crime and Cyber Security
月光博客
月光博客
Scott Helme
Scott Helme
T
Tor Project blog
有赞技术团队
有赞技术团队
AWS News Blog
AWS News Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Last Week in AI
Last Week in AI
S
Schneier on Security
Vercel News
Vercel News
博客园 - Franky
C
Cybersecurity and Infrastructure Security Agency CISA
L
LINUX DO - 热门话题
NISL@THU
NISL@THU
L
LangChain Blog
爱范儿
爱范儿
Google DeepMind News
Google DeepMind News
The GitHub Blog
The GitHub Blog
雷峰网
雷峰网
Latest news
Latest news
C
CXSECURITY Database RSS Feed - CXSecurity.com
Hugging Face - Blog
Hugging Face - Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
G
GRAHAM CLULEY
S
Security Affairs
A
About on SuperTechFans
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
大猫的无限游戏
大猫的无限游戏
W
WeLiveSecurity
Cisco Talos Blog
Cisco Talos Blog
罗磊的独立博客

博客园 - floerggyy

vs下调试多个c项目联调 C语言locale介绍 C语言宏定义总结 原码、反码、补码 Endian的由来及big-edian 和little-endian 转载 zip文件格式 C 语言 相关资源 精选 zlib usage Stdcall and DLL tools of MSVC and MinGW 转载 关于gcc的dlltool和dllwrap工具 技术转载(鼠标点击X窗口关闭IE的同时清空session,最基本的就是处理用户重复登陆需要用到,我想这个的关键在于如何捕捉到关闭IE这个动作,之后再根据自身的需要使用session.invalidate()或者session.removeAttribute( "xxx ")) dotnet oracle摘自msdn 被遗忘了的生日 这处站点真NB!! 数学表达式逻辑表达式混合计算 Are you a vender or manager? did you fix these bugs hotmail 附件上传何以如此霸道
一段垃圾的代码(附malloc,calloc和realloc使用小结)
floerggyy · 2008-02-17 · via 博客园 - floerggyy

char* encstrstostr(char **strs,char *dst)
{
            
if(!(*strs))
                
return NULL;
            
int j,siz;
            
char *nl;
            
char *st;
            st
=encstr(*strs);
            j
=strlen(st);
            siz
=j+1;
            dst
=malloc(siz);
            
if(dst)
            
{
                nl
=memcpy(dst,st,j);
                
while(*++strs)
                
{
                    st
=encstr(*strs);
                    nl
+=j;
                    j
=strlen(st);
                    siz
+=j+1;
                    
if(realloc(dst,siz))
                    
{
                        
*nl=' ';
                        nl
++;
                        memcpy(nl,st,j);
                    }

                    
else
                    
{
                        
goto errla;
                    }

                }

                nl
+=j;
                
*nl='\0';
                
return dst;
            }

            
else
             errla:
                
return NULL;
}


void* malloc(unsigned size); void* calloc(size_t nelem, size_t elsize); 和void* realloc(void* ptr, unsigned newsize);都在stdlib.h函数库内,是C语言的标准内存分配函数。
1. 函数malloc()和calloc()都可以用来动态分配内存空间。malloc()函数有
一个参数,即要分配的内存空间的大小,
malloc 在分配内存时会保留一定的空间用来记录分配情况,分配的次数越多,这些记录占用的空间就越多。另外,根据 malloc 实现策略的不同,malloc 每次在分配的时候,可能分配的空间比实际要求的多些,多次分配会导致更多的这种浪费。当然,这些都和 malloc 的实现有关;calloc()函数有两个参数,分别为元素的数目和每个元素的大小,这两个参数的乘积就是要分配的内存空间的大小。如果调用成功,它们都将返回所分配内存空间的首地址。
2. 函数malloc()和函数calloc()的主要区别是前者不能初始化所分配的内存
空间,而后者能。
3. realloc可以对给定的指针所指的空间进行扩大或者缩小,无论是扩张或
是缩小,原有内存的中内容将保持不变。当然,对于缩小,则被缩小的那一
部分的内容会丢失。
4. realloc 并不保证调整后的内存空间和原来的内存空间保持同一内存地址
。相反,realloc 返回的指针很可能指向一个新的地址。所以在代码中,我
们必须将realloc返回的值,重新赋值给 p :
p = (int *) realloc (p, sizeof(int) *15);