Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
malloc()返回一个指向充满随机字符的字符的指针_C - Fatal编程技术网

malloc()返回一个指向充满随机字符的字符的指针

malloc()返回一个指向充满随机字符的字符的指针,c,C,我用C语言编写了一个简单的程序,我正在尝试使两个集合相交(没有重复)。在我的交集结果之后会打印出一堆字符 这与strlen(scommon)=14而不是2(sizeof(char)*strlen(smin)=1*2)这一事实有多大关系 谢谢 代码如下: #include <stdio.h> #include <stdlib.h> #include <string.h> char* intersection(char *s1, char *s2); int

我用C语言编写了一个简单的程序,我正在尝试使两个集合相交(没有重复)。在我的交集结果之后会打印出一堆字符

这与strlen(scommon)=14而不是2(sizeof(char)*strlen(smin)=1*2)这一事实有多大关系

谢谢

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* intersection(char *s1, char *s2);

int main(void){


   printf("intersection = %s\n", intersection("ag", "cg"));

   return 0;
}

char* intersection(char *s1, char *s2){
    int i, j;
    char *smin, *scommon;   

    if(strlen(s1) < strlen(s2)){    
        smin = s1; 
        s1 = s2;
    }else{      
        smin = s2;      
    }

    scommon = (char*)malloc(sizeof(char) * strlen(smin));   

    for(i = 0; i < strlen(smin); i++){

        for(j = 0; j < strlen(s1); j++){

            if(smin[i] == s1[j]){

                scommon[i] = smin[i];

                break;
            }
        }
    }

    printf("final length = %d\n", strlen(scommon));     
    return scommon;
}
#包括
#包括
#包括
字符*交叉点(字符*s1,字符*s2);
内部主(空){
printf(“交叉点=%s\n”,交叉点(“ag”、“cg”);
返回0;
}
字符*交叉点(字符*s1,字符*s2){
int i,j;
char*smin,*scommon;
如果(strlen(s1)

您忘了使用null终止您的scommon字符串。您必须在最后一个之后添加“\0” 字符,否则strlen和printf将继续运行,直到它们遇到空字符,或使程序崩溃

通常,不能指望malloc返回包含所有0的缓冲区。如果希望缓冲区为“空”,则必须自己初始化,或者改用
calloc

scommon = (char*)malloc(sizeof(char) * strlen(smin));
这不会添加额外的字节来容纳字符串的空终止符,
intersection
不会在循环完成后添加空终止符

您可以将其更改为:

scommon = malloc(strlen(smin)+1);
memset( scommon, 0, strlen(smin)+1 );
或者在循环完成并且您知道要将其放置在何处时添加终止零

或者只需使用
calloc
即可为您初始化缓冲区:

scommon = calloc(strlen(smin)+1, sizeof(char));
我想主要的问题是(除了无效终止问题):

如果没有交叉口,
i
仍将增加。根据
malloc
功能描述:

新分配的内存块的内容未初始化,剩余值不确定


您应该为
scommon
使用单独的计数器。并添加一个空终止字符或将字符串长度保存在某个位置。

这不是
malloc
的错误。是的,
malloc
ed缓冲区的内容未指定。你的问题是什么?为什么当它只是
malloc(strlen(smin))
时,为什么要说一些冗长的话,比如
(char*)malloc(sizeof(char)*strlen(smin))
?请不要在C中强制转换
malloc
调用的返回值。空指针不需要强制转换。。。许多人认为铸造是一种不好的做法。另外,请阅读手册中的
malloc
:内存未初始化,使用
calloc
将返回干净的内存块。最后:你到底想做什么
memset
memcpy
在这方面有帮助。我怎样才能避免由于缺少cast(char*)而产生的错误呢?我试过了。在进入for循环之前,scommon的长度为14。如果'\0'字符的长度从开头算起更大,你怎么知道在哪里添加它呢?在循环的末尾,
scommon[i]='\0'
@ChrisJ.Kiick:如果您正在使用
calloc
,就不需要了,因为您在处理字符串时应该这样做……为什么您不使用
calloc
,它会为您节省一个额外的
strlen
调用:
scocommon=calloc(strlen(smin)+1,sizeof(*scocommon))的作用完全相同thing@EliasVanOotegem除了我没有想到之外,没有什么好的理由。我只是把注意力集中在OP示例代码中的
malloc
。谢谢你的推荐。
scommon[i] = smin[i];