放兩個字串后,它回傳我
segmention fault (core dumbed)
我正在用 gcc 運行代碼。Valgrind 寫道,沒有錯誤。嘗試撰寫使用 Caesar 方法解密文本的代碼。首先我輸入要解碼的文本,第二個文本顯示我需要將第一個文本中的字母移動多遠。即,選擇接收文本與第二輸入文本之間的字母對應更多的距離。我仔細檢查了所有內容,對我來說似乎真的沒有錯誤,或者我只是不精通記憶體。
這是示例
輸入
xUbbemehbT
XYlloworld
應該回傳
Helloworld
我真的不明白為什么它會回傳給我,這是代碼
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INT_MSG_LEN 50;
enum {NO_ERROR = 0,
ERROR_INPUT = 100,
ERROR_LEN = 101};
static const char *error_texts[] = { "Error input!",
"Error lenght"};
void shift(char *msgEnc, char *msg, char *msgRes, char *mainMsg, char *alphabet, int offset);
void report_error(int error);
void print_error(int error);
int get_sameletters(char *msg, char *msgRes, int offset);
int get_letter(char letter, char *alphabet);
int compare(char *msgEnc, char *msg, char *msgRes, char *alphabet, int offset);
char *read_Input_Msg(int *msglen);
int main(int argc, char *argv[])
{
int ret = NO_ERROR;
char *msgEnc, *msg, *msgRes, *mainMsg, alphabet[53] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
int msgEncLen, msgLen;
msgEnc = msg = msgRes = mainMsg = NULL;
msgEncLen = msgLen = 0;
msgEnc = read_Input_Msg(&msgEncLen);
if (msgEnc)
msg = read_Input_Msg(&msgLen);
if (msgEnc == NULL || msg == NULL)
ret = ERROR_INPUT;
else if (msgEncLen != msgLen)
ret = ERROR_LEN;
if (ret == NO_ERROR)
shift(msgEnc, msg, msgRes, mainMsg, alphabet, msgEncLen);
else
print_error(ret);
free(msgEnc);
free(msg);
free(msgRes);
free(mainMsg);
return ret;
}
void shift(char *msgEnc, char *msg, char *msgRes, char *mainMsg, char *alphabet, int offset)
{//function for decoding text by a defined offset
int dis;
dis = compare(msgEnc, msg, msgRes, alphabet, offset);
for (int i = 0; i<offset-1; i){
if ((msgEnc[i] >= 'a' && msgEnc[i] <= 'z') || (msgEnc[i] >= 'A' && msgEnc[i] <= 'Z')){
mainMsg[i] = msgEnc[i dis];
break;
}
}
for(int i = 0; i<offset-1; i)
printf("%c", mainMsg[i]);
}
void report_error(int error)
{//prints error
if (error >= ERROR_INPUT && error <= ERROR_LEN)
fprintf(stderr, "%s\n", error_texts[error - ERROR_INPUT]);
}
void print_error(int error)
{//what error it is
switch (error){
case ERROR_INPUT:
report_error(ERROR_INPUT);
break;
case ERROR_LEN:
report_error(ERROR_LEN);
break;
}
}
int get_sameletters(char *msg, char *msgRes, int offset)
{//gets count of sameletters between two strings
int sameLetters = 0;
for (int i = 0; i<offset-1; i){
if (msg[i] == msgRes[i])
sameLetters ;
}
return sameLetters;
}
int get_letter(char letter, char *alphabet)
{
int k;
for (int i=0; alphabet[i]; i){
if (letter == alphabet[i])
k = i;
}
return k;
}
int compare(char *msgEnc, char *msg, char *msgRes, char *alphabet, int offset)
{//calculate a distance between first input string and string what will get after decryption
int distance, max = 0;
for (int i = 0; alphabet[i]; i){
for (int j = 0; msgEnc[j]; j){
if ((msgEnc[i] >= 'a' && msgEnc[i] <= 'z') || (msgEnc[i] >= 'A' && msgEnc[i] <= 'Z'))
msgRes[j] = alphabet[(get_letter(msgEnc[j], alphabet) i) % 52];
}
int sameLetters = get_sameletters(msg, msgRes, offset);
if (sameLetters > max){
max = sameLetters;
distance = i;
}
}
return distance;
}
char *read_Input_Msg(int *msglen)
{//input messages, at the same time counts the length of the entered string
int capacity = INT_MSG_LEN;
char *msg = malloc(capacity);
int c, len = 0;
while ((c = getchar()) != EOF && c != '\n'){
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))) {
free(msg);
msg = NULL;
len = 0;
break;
}
if (len == capacity){
char *tmp = realloc(msg, capacity * 2);
if (tmp == NULL){
free(msg);
msg = NULL;
len = 0;
break;
}
capacity *= 2;
msg = tmp;
}
msg[len ] = c;
}
*msglen = len;
return msg;
}
uj5u.com熱心網友回復:
你失敗了,因為你用 msgres == null 呼叫 shift
msgEnc = msg = msgRes = mainMsg = NULL; <<<<==========
msgEncLen = msgLen = 0;
msgEnc = read_Input_Msg(&msgEncLen);
if (msgEnc)
msg = read_Input_Msg(&msgLen);
if (msgEnc == NULL || msg == NULL)
ret = ERROR_INPUT;
else if (msgEncLen != msgLen)
ret = ERROR_LEN;
if (ret == NO_ERROR)
shift(msgEnc, msg, msgRes, mainMsg, alphabet, msgEncLen); <<<<======
uj5u.com熱心網友回復:
這是 valgrind 抱怨,我知道這不是答案,但您的 valgrind 似乎有問題,也許這會有所幫助
==7662== Invalid write of size 1
==7662== at 0x1096C9: compare (hello.c:110)
==7662== by 0x1093CF: shift (hello.c:55)
==7662== by 0x109338: main (hello.c:41)
==7662== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==7662==
==7662==
==7662== Process terminating with default action of signal 11 (SIGSEGV)
==7662== Access not within mapped region at address 0x0
==7662== at 0x1096C9: compare (hello.c:110)
==7662== by 0x1093CF: shift (hello.c:55)
==7662== by 0x109338: main (hello.c:41)
110是這條線
msgRes[j] = alphabet[(get_letter(msgEnc[j], alphabet) i) % 52];
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/449837.html
上一篇:“運行google-github-actions/setup-gcloud@master”上的Github操作錯誤。如何解決這個問題?
