# why my c code get Memory Limit Exceeded......

• I could not find out that why my c code get Memory Limit Exceeded after i submiting, anyone can help?

``````struct HashMap
{
int count;
int val;
struct HashMap *next;
} * disHashMap[5];

void addToHashMap(int key) {
int slot = key % 5;
struct HashMap *p = disHashMap[slot];

while (p != NULL) {
if (p->val == key) {
p->count++;

return;
}
p = p->next;
}

p = disHashMap[slot];
disHashMap[slot] = (struct HashMap *)malloc(sizeof(struct HashMap));
disHashMap[slot]->val = key;
disHashMap[slot]->count = 1;
disHashMap[slot]->next = p;
}

int getDistance(int* a, int *b) {
return abs(a[0] - b[0]) * abs(a[0] - b[0]) + abs(a[1] - b[1]) * abs(a[1] - b[1]);
}

int numberOfBoomerangs(int** points, int pointsRowSize, int pointsColSize) {
int i, j, k;
int ret = 0;

struct HashMap *p, *toBeFree;

for (i = 0; i < pointsRowSize; i++) {

for (k = 0; k < 5; k++) {
disHashMap[k] = NULL;
}

for (j = 0; j < pointsRowSize; j++) {
if (i == j) {
continue;
}

addToHashMap(getDistance(points[i], points[j]));
}

for (k = 0; k < 5; k++) {
p = disHashMap[k];

while (p != NULL) {
if (p->count > 1) {
ret += p->count * (p->count - 1);
}
p = p->next;
}
}

for (k = 0; k < 5; k++) {
p = disHashMap[k];
if ( p != NULL) {
toBeFree = p;
p = p->next;
free(toBeFree);
}
disHashMap[k] = NULL;
}

}

return ret;
}`````````

• I guess my code use malloc too many times，malloc use heap space which is limited by leetcode。I change disHashMap to array(it use stack area)，and it finally got accepted。

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.