1

I am doing a Caesar cipher program in C. I already did the program but sometimes I get errors when running it.

Code:

#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;
}

I commented freeing allocated space because it was also giving me an error. I wanted to deal with it later.

All printfs excpet the last one are just for my control to know what this code does, and where it stops.

Sipmly code: I get 2 string in str and str2 with getchar. (Var al and ak are insted of usual i.) Then if they are bigger than (size(10)-2) I reallocate size+10. Then I work with strings and make the difference between chars. Then I look for the most used difference and use it as final rotation. After that I just rotate my 1st string with calculated rotation.

Input:

qrstuvwxyzABCDEFGHIJKLMNnop
aHcQefghWjdlmnopqostuvTxyYZ

Output:

DDDD
DDDD
Or1 19 Al 27
Af 19 Al 27

Error:

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

Excpected Output:

abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRS

Guillaume Jacquenot
  • 11,217
  • 6
  • 43
  • 49
W. White
  • 11
  • 1

2 Answers2

2

In your first loop, you realloc str, but don't assign the new value back into str, although you do access what str points to afterwards. If realloc moved it (which it is allowed to do), this would cause a problem. Similar thing w/ str2 in the second loop.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
0

Here:

char *strR=(char *) realloc(str,size);

you are reallocating str, but saving the new memory segment on strR. realloc may have to move the data, so the str pointer is potentially invalid.

giusti
  • 3,156
  • 3
  • 29
  • 44
  • 1
    The cast is not required - it is bad - see http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – Ed Heal Dec 03 '16 at 22:15