0

I have the following function:

int* getDifference(char* s1, char* s2){
    int* difference = (int*) malloc(sizeof(s1) / sizeof(s1[0]) * sizeof(int));
    for(int i = 0; i < strlen(s1); ++i){
        difference[i] = (s1[i] - s2[i] + 26) % 26;
    }
    return difference;
}

If I try to call it using:

char* encryptedSignature = "hello";
char* signature = "volvo";
int* key = getDifference(encryptedSignature, signature);
free(key);

It results in the following error on runtime:

/bin/sh: line 1: 10508 Segmentation fault: 11 ./code < input001.txt make: *** [code] Error 139

Why does this happen? Is there another way this should be done?

EXTRA:

If I use Sublime Text to build, I get:

code(10470,0x7fffdf5f83c0) malloc: *** error for object 0x7fea1d402648: incorrect checksum for freed object - object was probably modified after being freed. *** set a breakpoint in malloc_error_break to debug /bin/sh: line 1: 10470 Abort trap: 6 ./code < input001.txt make: *** [code] Error 134

GIZ
  • 4,409
  • 1
  • 24
  • 43

0 Answers0