Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.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
C 字符数组中的杂散字符_C_Malloc - Fatal编程技术网

C 字符数组中的杂散字符

C 字符数组中的杂散字符,c,malloc,C,Malloc,我对这段代码有一个问题,这段代码是一个铁路密码的加密,如果你输入一个“测试”,你应该得到一个输出“tietnsg”,我得到了 但是,如果我将输入更改为“testingj”,我会得到一个“tietnjsgp?²!ц‰”的输出,我可以从调试中看到“?²!ц‰”在上次填充toCipher函数时似乎被标记为打开 除了我做的方式,还有谁知道如何修复它吗 /* CIS Computer Secutrity Program 1 10-10-14 */ #include <stdio.h&

我对这段代码有一个问题,这段代码是一个铁路密码的加密,如果你输入一个“测试”,你应该得到一个输出“tietnsg”,我得到了

但是,如果我将输入更改为“testingj”,我会得到一个“tietnjsgp?²!ц‰”的输出,我可以从调试中看到“?²!ц‰”在上次填充
toCipher
函数时似乎被标记为打开 除了我做的方式,还有谁知道如何修复它吗

/*
    CIS Computer Secutrity Program 1
    10-10-14
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>

char *toCipher(char **arr,int x,int y);
char *Encrypt(char *pT, int size);
char **create(int x,int y);
void FreeArr(char **array, int y);
void print(char *word,int strl);

int main(){
    char pt[]= "testingj";
    char *word = Encrypt(pt,3);
    print(word, sizeof(pt));
    free(word);

}

/*
Take in a pointer to a word, and the lenght of the string
Post print each char in the array, (used beacuase i had some issues with the memory, i keep getting extra adresses
*/
void print(char *word,int strl){
    int i;
    for(i=0;i<strl-1;i++){
        printf("this is correct %c",word[i]);
    }
    printf("\n");
}

/*
Pre, take in the pointer to the plain text word to be encrypted as well as the depth of the Encryption desired
Post: Construct the array, insert values into the 2d array, convert the 2d array to a 1d array and return the 1d array
*/

char *Encrypt(char *word,int y){
    int x = strlen(word);
    int counter=0;
    int ycomp=0;
    int rate=1;

    char **rail = create(x,y);

        while(counter<x){   
            if(ycomp==y-1){
                rate=-1;
            }
            if(ycomp==0){
                rate=1;         
            }
            rail[counter][ycomp]=word[counter]; 
            ycomp=ycomp+rate;
            counter++;
        }//end of rail construction

    char *DrWord = toCipher(rail,x,y);  

    FreeArr(rail,y);    
    return(DrWord);
}


/*
Create a dynamic 2d array of chars for the rail cypher to use
Take in the dimensions
return the pointer of the rails initial address, after it created the space for the rail
*/


char *toCipher(char **arr,int x,int y){
    int xI =0;
    int yI=0;
    int counter =0;
    char *word = (char*)malloc(x);
    int i;


    for(yI=0;yI<y;yI++){    
        for(xI=0;xI<x;xI++){
            if(arr[xI][yI]!= 0){                    
                word[counter]=arr[xI][yI];
                counter++;
            }
        }

    }
        printf("this is the problem %s\n",word);
        return(word);
}




char **create(int x, int y){
    char **rail;            
    int i,j;

    rail = malloc(sizeof(char**)*x);

    for(i=0;i<x;i++){
        rail[i]= (char*)malloc(y * sizeof(char*));
    }

    for(i=0;i<y;i++){
        for(j=0;j<x;j++){
            rail[j][i]= 0;
        }
    }

    return(rail);
}


/*
Pre, take in a malloc'd array, with the height of the array 
free the malloc calls one by one, and finally free the initial adress
*/
void FreeArr(char **array, int y){
    int i;
    for(i=0;i<y;i++){
        free(array[i]);
    }
    free(array);
}
/*
CIS计算机安全程序1
10-10-14
*/
#包括
#包括
#包括
#包括
char*toCipher(char**arr,int x,int y);
char*加密(char*pT,int size);
字符**创建(整数x,整数y);
void FreeArr(字符**数组,整数y);
无效打印(字符*字,整数字符串);
int main(){
字符pt[]=“testingj”;
char*word=Encrypt(pt,3);
印刷品(文字、尺寸(pt));
免费(字);
}
/*
输入一个指向单词的指针,以及字符串的长度
对数组中的每个字符进行后期打印(因为我的内存有问题,所以会不断获得额外的地址)
*/
无效打印(字符*字,整数字符串){
int i;

对于
toCipher
中的(i=0;i),打印
字时不带nul终止符。或者:

char *word = (char*)malloc(x+1); // allocate an extra char for nul.
word[x] = 0; // add the nul at the end.
或:


我忘了将word初始化为0,如果您在调试模式下观察它,标记的内存没有被替换,因此在新构造的字符串中被标记了

虽然很可能不是问题的原因,但这
char**rail;rail=malloc(sizeof(char**)*x);
is至少应该是
…rail=malloc(sizeof(char*)*x)或者更好:
…rail=malloc(x*sizeof*rail);
我相信
char*word=(char*)malloc(x);
toCipher
中应该是
char*word=malloc(sizeof(word)*x)
,但我可能是错的。通过任何方式分配目标指向的N倍,所以它应该是
char word=malloc(x*sizeof*word);
@Namfuak@alk我知道我遗漏了什么。用这个“char-word=malloc(xsizeof(word));”我一直得到一个带标记的内存块。在我的arraymy解决方案的末尾添加了MSC标记。我的解决方案是将word中的字符初始化为null,但这也可以工作,字符在null字符处终止,或者在您告诉它停止的地方终止
printf("this is the problem %.*s\n",x,word); // limit characters printed to x.