struct hashmap {
int key;
int val;
};
int hash_find(int num, int size, struct hashmap* hash) {
int k, i;
for(i = 0; i < size; i++) {
k = (num + i) % size;
if(hash[k].key == num) return hash[k].val;
}
return -1;
}
void hash_add(int num, int size, int index, struct hashmap* hash) {
int k, i;
for(i = 0; i < size; i++) {
k = (num + i) % size;
if(hash[k].key == -9999 && hash[k].val == -9999) {
hash[k].key = num;
hash[k].val = index;
return;
}
}
}
int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
int* res = calloc((*returnSize = 2), sizeof(int));
struct hashmap* hash = calloc(numsSize, sizeof(struct hashmap));
int i;
for(i = 0; i < numsSize; i++) {
hash[i].key = -9999;
hash[i].val = -9999;
}
for(i = 0; i < numsSize; ++i) {
int k = target - nums[i];
int result = hash_find(k, numsSize, hash);
if(result >= 0) {
res[0] = result;
res[1] = i;
break;
} else {
hash_add(nums[i], numsSize, i, hash);
}
}
free(hash); // Free allocated memory for hash
return res;
}
it gives address sanitizer error and highlights this line if(hash[k].key == num) return hash[k].val; when run on leetcode with testcase [0,4,3,0] with target 0.
** i have created hash table with linear probing**
it gives address sanitizer error when run on leetcode with testcase [0,4,3,0] with target 0.
it gives address sanitizer error when run on leetcode with testcase [0,4,3,0] with target 0.
it gives address sanitizer error when run on leetcode with testcase [0,4,3,0] with target 0.
it gives address sanitizer error when run on leetcode with testcase [0,4,3,0] with target 0.
it gives address sanitizer error when run on leetcode with testcase [0,4,3,0] with target 0.
it gives address sanitizer error when run on leetcode with testcase [0,4,3,0] with target 0.
it gives address sanitizer error when run on leetcode with testcase [0,4,3,0] with target 0.
it gives address sanitizer error when run on leetcode with testcase [0,4,3,0] with target 0.
it gives address sanitizer error when run on leetcode with testcase [0,4,3,0] with target 0.
it gives address sanitizer error when run on leetcode with testcase [0,4,3,0] with target 0.