c - 使用 malloc 和 realloc 时出错

标签 c malloc realloc

我正在用 C 编写凯撒密码程序。我已经编写了该程序,但有时在运行时会出错。

代码:

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


int main() {

    int size=10,al=0;
    int sizek=10,ak=0;
    char *str= (char *) malloc(size+5);
    if (str == NULL) {
        printf("malloc error\n");
        return 0;
    }
    char *strIni=str;
    char *str2= (char *) malloc(sizek+5);
    if (str2 == NULL) {
        printf("malloc error\n");
        return 0;
        }
    char *str2Ini=str2;
    //char a,b;
    while ((str[al]=getchar())!='\n') {
        if (al==size-2){
            size=size+10;
            char *strR=(char *) realloc(str,size);
            if (strR == NULL) {
                printf("malloc error\n");
                return 0;
            }
            printf("DDDD\n");
            strIni=strR;
        }
        al++;
    }
    printf("Or1 %d Al %d\n",strlen(strIni),al );
    str[al]='\0';
    printf("Af %d Al %d\n",strlen(strIni),al );


    while ((str2[ak]=getchar())!='\n') {
        if (ak==sizek-2){
            sizek=sizek+10;
            char *str2R=(char *) realloc(str2,sizek+5);
            if (str2R == NULL) {
                printf("malloc error\n");
                return 0;
            }
            printf("CCCC\n");
            str=str2R;
        }
        ak++;
    }
    printf("Or2 %d Ak %d\n",strlen(str2Ini),ak );
    str2[ak]='\0';
    printf("Af2 %d Ak %d\n",strlen(str2Ini),ak );


    printf("Str1 %s\n",strIni );
    printf("Str2 %s\n",str2Ini );

    int sDup=1;
    int dif[strlen(str2Ini)];
    int* dup=(int *) malloc(sizek);
    int max[(sDup)];
    int rot[2]={0,0};

    for (int i=0;i<(strlen(str2Ini));i++){   //pokud AA - aa -xy)
        if (str2Ini[i]<123&&str2Ini[i]>96 &&strIni[i]<91&&strIni[i]>64){
            dif[i]=(int)str2Ini[i]-((int)strIni[i]+6);
        } else if (strIni[i]<123&&strIni[i]>96 &&str2Ini[i]<91&&str2Ini[i]>64){
            dif[i]=(int)strIni[i]-((int)str2Ini[i]+6);
        } else if (strIni[i]<123&&strIni[i]>96 &&str2Ini[i]<123&&str2Ini[i]>96) {
            dif[i]=(int)strIni[i]-((int)str2Ini[i]);
        } else if (strIni[i]<91&&strIni[i]>64 &&str2Ini[i]<91&&str2Ini[i]>64){
            dif[i]=(int)strIni[i]-((int)str2Ini[i]);
        }
        if (dif[i]<0) {
            dif[i]=-1*dif[i];
        }
        printf("Dif%d: %d\n",i,dif[i]);
    }
    for (int i=0;i<strlen(strIni);i++) {
        int l=0;
        for (int j=0;j<sDup;j++) {
            if (dif[i]==dup[j]) {
                max[j]++;
                l++;
                break;
            }
        }
        if (l==0){
            dup[sDup-1]=dif[i];
            max[sDup-1]=0;
            max[sDup-1]+=1;
            sDup++;
        }
    }
    for (int h=0;h<1;h++){
        for (int i=0;i<(sDup-1);i++) {
            if(rot[0]>max[i]) {
                rot[0]=rot[0];
            } else {
                rot[0]=max[i];
                rot[1]=dup[i];
            }
        }
    }
    for (int i=0;i<strlen(strIni);i++){
        if (((int)strIni[i]>64 && (int)strIni[i]<91) || ((int)strIni[i]>96 && (int)strIni[i]<123)){
            continue;
        } else {
            fprintf(stderr, "Error: Chybny vstup!\n");/*free(strIni);free(str2Ini);free(dup); */return 100;
        }
    }
    for (int i=0;i<strlen(strIni);i++){
        if (((int)strIni[i]>64 && (int)strIni[i]<91) || ((int)strIni[i]>96 && (int)strIni[i]<123)){
            if (strlen(strIni)==(strlen(str2Ini))) {
                if ((int)strIni[i]+(int)rot[1]>90 && (int)strIni[i]<91) {
                    strIni[i]=strIni[i]+6+(int)rot[1];
                } else if ((int)strIni[i]+(int)rot[1]>122 && (int)strIni[i]<123) {
                    strIni[i]=(strIni[i]-58+rot[1]);
                } else if ((int)strIni[i]>64 && ((int)strIni[i]+rot[1])<91) {
                    strIni[i]=strIni[i]+rot[1];
                } else if ((int)strIni[i]>96 && ((int)strIni[i]+rot[1])<123) {
                    strIni[i]=strIni[i]+rot[1];
                } else {
                    strIni[i]='#';
                }
                //printf("%c ",strIni[i] );
            } else {fprintf(stderr, "SSError: Chybna delka vstupu!\n");/*free(strIni);free(str2Ini);free(dup);*/ return 101;
            }
    } else {
    fprintf(stderr, "Error: Chybny vstup!\n");/*free(strIni);free(str2Ini);free(dup);*/ return 100;
    }
    } 
    str[al]='\0';
    printf("Rot: %d\n",rot[1] );
    printf("String: %s\n",strIni );
    // free(strIni);free(str2Ini);free(dup);
    //freeIni, dup
    ///posun o 42pismen
    return 0;
}

我评论释放分配的空间,因为它也给我一个错误。我想稍后再处理。

除了最后一个之外的所有 printfs 都只是为了让我知道这段代码做了什么,以及它在哪里停止。

简单的代码:我用 getchar 在 str 和 str2 中得到 2 个字符串。 (Var al 和 ak 是通常的 i。)然后如果它们大于 (size(10)-2) 我重新分配 size+10。然后我使用字符串并区分字符。然后我寻找最常用的差异并将其用作最终轮换。之后,我只是用计算好的旋转来旋转我的第一个弦。

输入:

qrstuvwxyzABCDEFGHIJKLMNnop
aHcQefghWjdlmnopqostuvTxyYZ

输出:

DDDD
DDDD
Or1 19 Al 27
Af 19 Al 27

错误:

prog: malloc.c:2842: mremap_chunk: Assertion `((size + offset) & (_rtld_global_ro._dl_pagesize - 1)) == 0' failed.

预期输出:

abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRS

最佳答案

在您的第一个循环中,您重新分配了 str,但没有将新值分配回 str,尽管您确实访问了什么str 指向后面。如果 realloc 移动它(这是允许的),这将导致问题。与第二个循环中的 str2 类似。

关于c - 使用 malloc 和 realloc 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40952722/

相关文章:

c - C 中的位掩码范围

C - 传递分配的指针并在单独的函数中使用 realloc

用emscripten编译SDL/SDL2导致重定义错误

从程序集中调用 libc 函数

c - 主要 : malloc. c :2372: sysmalloc: Assertion . .. 失败

malloc - CUDA 内核中的内存分配

c - 重新分配变体

c - realloc() 无效的下一个大小 - 再一次,我知道

c - 从 bin 文件 c 的缓冲区解析数据

c - 出现段错误 11 不确定在 C 中是否正确使用指针