c 实现字符串反转(参考redis源码)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _dys
{
    unsigned int len;
    unsigned int free;
    char buf[];
}dys;
char *dynewlen(const void *prt,size_t initlen)
{
    dys *ds=NULL;
    if(prt)
    {
        ds=(dys *)malloc(sizeof(*ds)+initlen+1);
    }
    else
    {
        ds=(dys *)calloc(sizeof(*ds)+initlen+1,1);
    }
    ds->len=initlen;
    ds->free=0;
    if(prt&&initlen)
    {
        memcpy(ds->buf,prt,initlen);
    }
    ds->buf[initlen]='\0';
    return (char *)ds->buf;
}
char *dyreverse(const void *prt,size_t initlen)
{
    dys *ds=NULL;
    if(prt)
    {
        ds=(dys *)malloc(sizeof(*ds)+initlen+1);
    }
    else
    {
        ds=(dys *)calloc(sizeof(*ds)+initlen+1,1);
    }
    ds->len=initlen;
    ds->free=0;
    int i;
    if(prt&&initlen)
    {
        for(i=initlen;i>=1;i--)
        {
            memcpy((char *)&ds->buf[initlen-i],(prt+i-1),1);
        }
    }
    ds->buf[initlen]='\0';
    return (char *)ds->buf;
}
int main(void)
{
    char *s="hello,123";
    int i,slen=strlen(s),j;
    dys *args[1024]={NULL};
    char *prt=NULL;
    printf("###############normal##################\n");
    for(i=0;i<=slen;i++)
    {
        prt=dynewlen(s,i);
        args[i]=(dys *)(prt-sizeof(dys));
        printf(" i =%d, s= %s,len=%d\n",i,prt,args[i]->len);
    }
    j=i;
    printf("################reverse##################\n");
    for(i=0;i<=slen;i++)
    {
        prt=dyreverse(s,i);
        args[i+j]=(dys *)(prt-sizeof(dys));
        printf(" i =%d, s= %s,len=%d\n",i+j,prt,args[i+j]->len);
    }
    for(i=0;i<1024;i++)
    {
        if(args[i]!=NULL)
        {
            printf(" can free args[%d] addr =%p\n",i,args[i]);
            free(args[i]);
            args[i]=NULL;
        }
    }
    return 0;
}


运行结果:

[mysql@centos2 sredis]$ ./a.out
###############normal##################
 i =0, s= ,len=0
 i =1, s= h,len=1
 i =2, s= he,len=2
 i =3, s= hel,len=3
 i =4, s= hell,len=4
 i =5, s= hello,len=5
 i =6, s= hello,,len=6
 i =7, s= hello,1,len=7
 i =8, s= hello,12,len=8
 i =9, s= hello,123,len=9
################reverse##################
 i =10, s= ,len=0
 i =11, s= h,len=1
 i =12, s= eh,len=2
 i =13, s= leh,len=3
 i =14, s= lleh,len=4
 i =15, s= olleh,len=5
 i =16, s= ,olleh,len=6
 i =17, s= 1,olleh,len=7
 i =18, s= 21,olleh,len=8
 i =19, s= 321,olleh,len=9
 can free args[0] addr =0x13d3010
 can free args[1] addr =0x13d3030
 can free args[2] addr =0x13d3050
 can free args[3] addr =0x13d3070
 can free args[4] addr =0x13d3090
 can free args[5] addr =0x13d30b0
 can free args[6] addr =0x13d30d0
 can free args[7] addr =0x13d30f0
 can free args[8] addr =0x13d3110
 can free args[9] addr =0x13d3130
 can free args[10] addr =0x13d3150
 can free args[11] addr =0x13d3170
 can free args[12] addr =0x13d3190
 can free args[13] addr =0x13d31b0
 can free args[14] addr =0x13d31d0
 can free args[15] addr =0x13d31f0
 can free args[16] addr =0x13d3210
 can free args[17] addr =0x13d3230
 can free args[18] addr =0x13d3250
 can free args[19] addr =0x13d3270

 

 

 

posted @ 2015-08-07 15:22  一个万能盒子叫数据库  阅读(245)  评论(0编辑  收藏  举报