memory management - Why am I unable to allocate space using malloc in C? -
this question has answer here:
- c, finding length of array inside function 7 answers
- why isn't size of array parameter same within main? 13 answers
- different sizeof results 4 answers
- find size of string pointed pointer 7 answers
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
Post a Comment