内存分配,输出垃圾
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了内存分配,输出垃圾相关的知识,希望对你有一定的参考价值。
为什么在arrayLetter中输出垃圾而不是char值?当我把for循环打印到arrayLetter上面时,方法调用它的工作原理;当我把它放在它下面时,它不起作用。我没有在方法或主要方面更改arrayLetter。这是一个简单的程序,可以移动索引并输出移位的字母表。
#include <stdio.h>
#include <stdlib.h>
int* getMatchedIndex(char arrayLetter[], char plainTextExample[],int sizeArrayLetter, int sizePlainTextExample);
int* vigenereCipherEncryptIndex(int key[], int sizeKey, int sizeArrayLetter, int sizePlainTextExample, int arrayMatchAtIndex[]);
char* convertToNewEncryptedArray(char arrayLetter[], int encryptArray[], int sizeArrayLetter, int sizePlainTextExample);
int main(void) {
char arrayLetter[] = { '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'};
int key[] = { 21, 4, 2, 19, 14, 17};
char plainTextExample[] = { 'h', 'e', 'r', 'e',
'i', 's',
'h', 'o', 'w',
'i', 't',
'w', 'o', 'r', 'k', 's'};
int sizeArrayLetter = sizeof(arrayLetter)/sizeof(arrayLetter[0]);
int sizePlainTextExample = sizeof(plainTextExample)/sizeof(plainTextExample[0]);
int sizeKey = sizeof(key)/sizeof(key[0]);
int *arrayMatchAtIndex;
int *encryptArray;
char *cipherArray;
arrayMatchAtIndex = getMatchedIndex(arrayLetter, plainTextExample, sizeArrayLetter, sizePlainTextExample);
encryptArray = vigenereCipherEncryptIndex(key, sizeKey, sizeArrayLetter, sizePlainTextExample, arrayMatchAtIndex);
cipherArray = convertToNewEncryptedArray(arrayLetter, encryptArray, sizeArrayLetter, sizePlainTextExample);
for(char i=0; i < sizeArrayLetter; i++)
{
//printf("%s%i%s", "Indexes of Encryption: ", encryptArray[i], "
");
printf("%s", arrayLetter);
}
free(arrayMatchAtIndex);
free(encryptArray);
free(cipherArray);
return EXIT_SUCCESS;
}
//compare arrays and store index into array
int *getMatchedIndex(char arrayLetter[], char plainTextExample[],int sizeArrayLetter, int sizePlainTextExample)
{
int* arrayMatchAtIndex = malloc(sizePlainTextExample* sizeof(int));
//loop plainTextExample
for(int i=0; i < sizePlainTextExample ; i++)
{
//loop arrayLetter
for(int j=0; j < sizeArrayLetter ; j++)
{
//compare -> match then store arrayLetter index into arrayMatchAtIndex
if(plainTextExample[i] == arrayLetter[j])
{
arrayMatchAtIndex[i] = j;
}
}
}
return arrayMatchAtIndex;
}
//encrypt array by adding 'key' indexes into array
int* vigenereCipherEncryptIndex(int key[], int sizeKey, int sizeArrayLetter, int sizePlainTextExample, int arrayMatchAtIndex[])
{
//make new array
int* encryptArray = malloc(sizePlainTextExample* sizeof(int));
int j=0;
//encrypt element -> (add elements of arrayIndexes and key) mod 26
for(int i=0; i < sizePlainTextExample; i++)
{
if(i >= sizeKey)
{
j = (i % sizeKey);
key[i] = key[j];
}
encryptArray[i] = (arrayMatchAtIndex[i]+key[i]) % 26;
// printf("%s%d%s%d%s", "Match Element: ", arrayMatchAtIndex[i], " key ", key[i], " ");
// printf("%s%d%s", "encryptElement: ", encryptArray[i], "
");
}
return encryptArray;
}
//convert encrypt array index to its elements
char* convertToNewEncryptedArray(char arrayLetter[], int encryptArray[], int sizeArrayLetter, int sizePlainTextExample)
{
char* cipherArray = malloc(sizePlainTextExample* sizeof(int));
//encrypted index - > convert elements to new index
for(int i=0; i < sizePlainTextExample ; i++)
{
for(int j=0; j< sizeArrayLetter; j++)
{
//find index->match then put Encrypted index into letter element
if(encryptArray[i] == j)
{
cipherArray[i] = arrayLetter[j];
printf("%c", arrayLetter[2]);
}
}
}
return cipherArray;
}
答案
正如Jonathan Leffler所说,你的数组只是一个数组,而不是C字符串(字符序列为nul-terminated)。仅将%s
用于C字符串。
将你的循环改为:
for (char i=0; i<sizeArrayLetter; i++) {
putchar(arrayletter[i]);
}
putchar('
');
这将单独打印阵列中的所有字符和一个额外的行尾。
以上是关于内存分配,输出垃圾的主要内容,如果未能解决你的问题,请参考以下文章