calloc=malloc+memset?

功能上calloc=malloc+memset;
从汇编指令来看的话,calloc的指令是malloc+memset的一半左右。
时间上来说,calloc节省时间
用oProfile测试,重复10000000次,使用CPU cycle为(不用看绝对,相对比较就好)
   16.3547 :                        c=calloc(100,i);

   8.0788+18.6207 :        m=malloc(100*i); +   memset(m,0,100*i);

因此,实际中,应该根据自己的需求和性能要求来选择。不需要memset的地方就不用多此一举。

 
下面是汇编代码
m=malloc(100*i);
   0x0000000000400563 <+15>: mov    -0x14(%rbp),%eax
   0x0000000000400566 <+18>: imul   $0x64,%eax,%eax
   0x0000000000400569 <+21>: cltq   
   0x000000000040056b <+23>: mov    %rax,%rdi
   0x000000000040056e <+26>: callq  0x400440 <malloc@plt>
   0x0000000000400573 <+31>: mov    %rax,-0x10(%rbp)
 memset(m,0,100*i);
   0x0000000000400577 <+35>: mov    -0x14(%rbp),%eax
   0x000000000040057a <+38>: imul   $0x64,%eax,%eax
   0x000000000040057d <+41>: movslq %eax,%rdx
   0x0000000000400580 <+44>: mov    -0x10(%rbp),%rax
   0x0000000000400584 <+48>: mov    $0x0,%esi
   0x0000000000400589 <+53>: mov    %rax,%rdi
   0x000000000040058c <+56>: callq  0x400430 <memset@plt>
 
c=calloc(100,i);
   0x0000000000400591 <+61>: mov    -0x14(%rbp),%eax
   0x0000000000400594 <+64>: cltq   
   0x0000000000400596 <+66>: mov    %rax,%rsi
   0x0000000000400599 <+69>: mov    $0x64,%edi
   0x000000000040059e <+74>: callq  0x400460 <calloc@plt>
   0x00000000004005a3 <+79>: mov    %rax,-0x8(%rbp)
   0x00000000004005a7 <+83>: leaveq 
   0x00000000004005a8 <+84>: retq
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
static char *TCPCommand; static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; int SetUpTCPtoSendInformation(char option[], char **command) { static int isFirstCall = 1; pthread_mutex_lock(&mutex); // 加锁 if (isFirstCall && strstr(option, "set")) { if (TCPCommand != NULL) { free(TCPCommand); } TCPCommand = malloc(strlen(*command) + 1); if (TCPCommand == NULL) { printf("Failed to set and obtain TCP command variable memory allocation\n"); goto fail; } strcpy(TCPCommand, *command); printf("set:%s\n", TCPCommand); isFirstCall = 0; goto succeed; } else if (!isFirstCall && strstr(option, "get") && TCPCommand != NULL && strlen(TCPCommand)) { free(*command); *command = malloc(strlen(TCPCommand) + 1); strcpy(*command, TCPCommand); printf("get:%s\n", *command); memset(TCPCommand, '\0', strlen(TCPCommand)); free(TCPCommand); TCPCommand = NULL; isFirstCall = 1; goto succeed; } memset(*command, 0, sizeof(*command)); strcpy(*command, ""); fail: pthread_mutex_unlock(&mutex); // 解锁 return 0; succeed: pthread_mutex_unlock(&mutex); // 解锁 return 1; } (cmdBuf)=calloc(BUFFER_SIZE, sizeof(char)); if (ReceivePackets.cmdBuf == NULL) return 0; SetUpTCPtoSendInformation("set","sssssss"); char *resultbuf =NULL; SetUpTCPtoSendInformation("get",&resultbuf); if(strlen(resultbuf)){ free(cmdBuf); cmdBuf=malloc(strlen(resultbuf) + 1); strcpy(cmdBuf, resultbuf); } printf("%d %d %s %s", strlen(cmdBuf), strlen(resultbuf), cmdBuf, resultbuf); 这样子调用对么如果不对请帮我修改函数或者调用方法(SetUpTCPtoSendInformation("set","sssssss");这个不能修改),printf结果为多少
最新发布
07-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值