免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2200 | 回复: 4
打印 上一主题 下一主题

[C] 帮我看看内存分配的地方是否出错。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-01-02 19:04 |只看该作者 |倒序浏览
10可用积分
大家好,我写了个程序,作用是把一些文件进行base64编码并跟相关的字符封装起来返回。
主程序如下。但老是运行的时候出现段错误,我知道肯定是操作内存出了问题。可总不知道在什么地方,翻阅了一些基本知识,malloc后一定要进行free。
我想请教下面的问题:(不谈代码写的如何)
1、请各位指点下我这里内存操作是否正确,并指出原因;
2、(b_str=ss();的时候,看起来没有问题,事实上我也不清楚)我把b_str = ss(); 换成  b_str = base64_encode(str,len,&b_len); 运行就出现 段错误,
我发现在换成  b_str = base64_encode(str,len,&b_len); 的时候,去掉free(str);便可以执行,不出现段错误,但结果似乎有点问题,即 puts(b_str);b_str输出后面似乎多了点字符。肯定是操作内存什么地方出错了

恳请各位帮忙!

附件是我的全部程序,解决了再送8分

int main(){
    char *p[];
    p[0]    = "/www/b.c";
    p[1]    = "/www/c.c";
    p[2]    = "/www/encode.h";
    attachment(p);
}
static char* attachment(char**f){
    char **p,*str,filename[100],*ret;
    long i    = 1024,len=0;
    int b_len;
    char str1[SHORT_SIZE]    = "Content-Type: application/octet-stream; name=\"";
    char str2[SHORT_SIZE]    = "\"\nContent-Transfer-Encoding: BASE64\nContent-Disposition: attachment; filename=\"";
    FILE *fp;
    if(f==NULL)    return NULL;
    p    = f;
    ret    = (char*)malloc(1);   
    while(*p!=NULL){
        unsigned char *b_str;
        int j=0,k=0;
        str    = (char*)malloc(sizeof(char)*i+1);   
        bzero(str,i+1);
        fp    = fopen(*p,"rb");
        if(!fp){
            printf("can not open file %s\n",*p);
            exit(0);
        }
        len    += fread(str,sizeof(char),i,fp);
        while(!feof(fp)){  //     没有读完


            j++;
            str    = (char*)realloc(str,i*j+1);
            len+=fread(str+i*j,sizeof(char),i,fp);
        }
        *(str+i*j)    = '\0';
        bzero(filename,100);
        if(strrchr(*p,'/')!=0)
            strcpy(filename,strrchr(*p,'/')+1);
        else
            strcpy(filename,*p);
//        b_str    = base64_encode(str,len,&b_len);



        b_str    = ss();//  这行换成上面一句就出现 段错误


        k        = strlen(ret)+b_len+strlen(filename)*2+5+strlen(str1)+strlen(str2);
        ret    = (char*)realloc(ret,k+1);
        puts(b_str);   //  问题2说的这里


        strcat(ret,str1);strcat(ret,filename);
        strcat(ret,str2);strcat(ret,filename);
        strcat(ret,"\"\n\n");strcat(ret,b_str);
        strcat(ret,"\n\n");*(ret+k)    = '\0';
        free(b_str);
        free(str);
        len    = 0;
        p++;
    }
    return ret;
}

char * ss(){
    char *p;
    p    = (char*)malloc(101);
    bzero(p,101);
    strcpy(p,"okbt");
    return p;
}


下面是base64_encode.c ,我从php原码里弄出来的。

#include "base64_encode.h"
static const char base64_table[] =
    { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
      'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
      'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
      'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
      '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '\0'
    };

static const char base64_pad = '=';

static const short base64_reverse_table[256] = {
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
    52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
    -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
    15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
    -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
    41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
};

unsigned char *base64_encode(const unsigned char *str, int length, int *ret_length)
{
    const unsigned char *current = str;
    unsigned char *p;
    unsigned char *result;

    if ((length + 2) < 0 || ((length + 2) / 3) >= (1 << (sizeof(int) * 8 - 2))) {
        if (ret_length != NULL) {
            *ret_length = 0;
        }
        return NULL;
    }

    result = (unsigned char *)malloc(((length + 2) / 3) * 4);
    p = result;

    while (length > 2) { /* keep going until we have less than 24 bits */
        *p++ = base64_table[current[0] >> 2];
        *p++ = base64_table[((current[0] & 0x03) << 4) + (current[1] >> 4)];
        *p++ = base64_table[((current[1] & 0x0f) << 2) + (current[2] >> 6)];
        *p++ = base64_table[current[2] & 0x3f];

        current += 3;
        length -= 3; /* we just handle 3 octets of data */
    }

    /* now deal with the tail end of things */
    if (length != 0) {
        *p++ = base64_table[current[0] >> 2];
        if (length > 1) {
            *p++ = base64_table[((current[0] & 0x03) << 4) + (current[1] >> 4)];
            *p++ = base64_table[(current[1] & 0x0f) << 2];
            *p++ = base64_pad;
        } else {
            *p++ = base64_table[(current[0] & 0x03) << 4];
            *p++ = base64_pad;
            *p++ = base64_pad;
        }
    }
    if (ret_length != NULL) {
        *ret_length = (int)(p - result);
    }
    *p = '\0';
    return result;
}

[ 本帖最后由 sunceenjoy 于 2008-1-2 19:07 编辑 ]

my.tar.gz

4.26 KB, 下载次数: 47

论坛徽章:
0
2 [报告]
发表于 2008-01-02 19:16 |只看该作者
在p[2] = "/www/encode.h";
后面加上p[3] = NULL一句

论坛徽章:
0
3 [报告]
发表于 2008-01-02 22:31 |只看该作者
多谢楼上,忘记加这一句了,我明天验证下结果。
另外请回答下我的2个问题吧.我很想知道我到底有没有正确或错误,旁边一个问的人都没有.
学习中,最痛苦的事情莫过于想学习却没有一个指导老师!

论坛徽章:
0
4 [报告]
发表于 2008-01-02 22:33 |只看该作者
char *p[];
--------------
???

论坛徽章:
0
5 [报告]
发表于 2008-01-04 09:41 |只看该作者
把len的值打印出来,看看和strlen(str)是否相等
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP