memory management - Why am I unable to allocate space using malloc in C? -


i have following function:

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

if try call using:

char* encryptedsignature = "hello"; char* signature = "volvo"; int* key = getdifference(encryptedsignature, signature); free(key); 

it results in following error on runtime:

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

why happen? there way should done?

extra:

if use sublime text build, get:

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


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -