标题:如何解决malloc多分配的空间
只看楼主
兔先生
Rank: 2
等 级:论坛游民
帖 子:41
专家分:20
注 册:2018-11-9
结帖率:63.64%
已结贴  问题点数:15 回复次数:3 
如何解决malloc多分配的空间
malloc分配的空间在之后的使用中未完全使用完,如何解决?最后四行代码是什么意思?
程序代码:
char * addBinary(char * a, char * b){
    int  i,k,j,len;
    int  c;
    c=0;
    j=strlen(a);
    k=strlen(b);
    len=j>k?j:k;
    char *res=(char *)malloc(sizeof(char)*(len+2));
    res[len+1]='\0';
    i=len-1;
    while(i>=0)
    {
        if(j-1>=0&&k-1>=0){
        if(a[j-1]-'0'+b[k-1]-'0'+c==2)
        {
            res[i]='0';
            c=1;
        }else
        {
            res[i]=a[j-1]-'0'+b[k-1]-'0'+c;
            c=0;
        }}
        if(j-1<0)
        {
            if(b[k-1]-'0'+c==2)
            {
                res[i]='0';
                c=1;
            }else{
                res[i]=b[k-1]-'0'+c;
                c=0;
            }
        }
        if(k-1<0)
        {
            if(a[j-1]-'0'+c==2)
            {
                res[i]='0';
                c=1;
            }
            else{
                res[i]=a[j-1]-'0'+c;
                c=0;
            }
        }
        i--,j--,k--;
    } 
    if(c>0)
        res[0]='1';
    else 
        *res++;
    return res;
}

大佬要是有时间顺便解决一下为什么输出会有"10�"这个问号

[此贴子已经被作者于2019-12-8 17:12编辑过]

搜索更多相关主题的帖子: res 分配 空间 malloc char 
2019-12-08 17:10
兔先生
Rank: 2
等 级:论坛游民
帖 子:41
专家分:20
注 册:2018-11-9
得分:0 
#include<stdio.h>
#include<stdlib.h>
#include<string.h>


char* addBinary(char* a, char* b) {
    int  i, k, j, len;
    int  c;
    c = 0;
    j = strlen(a);
    k = strlen(b);
    len = j > k ? j : k;
    char* res = (char*)malloc(sizeof(char) * (len + 2));
    res[len + 1] = '\0';
    i = len - 1;
    while (i >= 0)
    {
        if (j - 1 >= 0 && k - 1 >= 0) {
            if (a[j - 1] - '0' + b[k - 1] - '0' + c == 2)
            {
                res[i] = '0';
                c = 1;
            }
            else
            {
                res[i] = a[j - 1] - '0' + b[k - 1] - '0' + c;
                c = 0;
            }
        }
        if (j - 1 < 0)
        {
            if (b[k - 1] - '0' + c == 2)
            {
                res[i] = '0';
                c = 1;
            }
            else {
                res[i] = b[k - 1] - '0' + c;
                c = 0;
            }
        }
        if (k - 1 < 0)
        {
            if (a[j - 1] - '0' + c == 2)
            {
                res[i] = '0';
                c = 1;
            }
            else {
                res[i] = a[j - 1] - '0' + c;
                c = 0;
            }
        }
        i--, j--, k--;
    }
    if (c > 0)
        res[0] = '1';
    else
        (*res)++;
    return res;
}

int main()
{
    char a[10] = "110";
    char b[10] = "1001";
    char* res;
    int i = 0;
    printf("%s",addBinary(a, b));
}

完整版代码
2019-12-08 18:44
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:15 
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* add_binary( const char* a, const char* b )
{
    size_t alen = strlen(a);
    size_t blen = strlen(b);
    size_t clen = (alen<blen?blen:alen)+1;
    char* c = malloc( clen+1 );
    c[clen] = '\0';

    const char* pa = a+alen;
    const char* pb = b+blen;
    char* pc = c+clen;
    char* pd = pc-1;
    for( char carry=0; pa>a||pb>b||carry!=0; )
    {
        if( pa > a )
            carry += *--pa - '0';
        if( pb > b )
            carry += *--pb - '0';
        *--pc = carry%2 + '0';
        carry /= 2;

        if( *pc == '1' )
            pd = pc;
    }

    memmove( c, pd, c+clen-pd+1 );
    return c;
}


#include <assert.h>

void test( const char* a, const char* b, const char* c )
{
    char* result = add_binary( a, b );
    if( strcmp(result,c) != 0 )
        printf( "%s + %s = %s (shouled be %s)\n", a, b, result, c );
    free( result );
}

int main( void )
{
    test( "0", "0", "0" );
    test( "1", "1", "10" );
    test( "000", "00000", "0" );
    test( "001", "00001", "10" );
    test( "1111", "11", "10010" );
    test( "11", "1111", "10010" );
}
2019-12-08 19:25
兔先生
Rank: 2
等 级:论坛游民
帖 子:41
专家分:20
注 册:2018-11-9
得分:0 
carry += *--pa - '0';
大佬能解释一下是怎么实现的吗?
 if (c > 0)
        res[0] = '1';
    else
        (*res)++;
还有这是什么意思?
2019-12-09 16:05



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-498285-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.319889 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved