it's the vizier of dynamic memory allocation reporting in again. I know it's generally frowned upon to provide the entire example, but I've been working through several problems on leetcode, and have found that I always have the same problem, and that is shadow bytes. I haven't been able to identify exactly what it is I'm doing wrong, because the error messages, and their driver program for the testcases is somewhat hidden, but if someone, for this simple example, could explain what I'm doing wrong, it would be appreciated:
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#define LOGLIST 100
typedef struct {
int * log_array;
} Logger;
/** Initialize your data structure here. */
int hash_message(char * message)
{
int sum = 0;
char * tmp = message;
int i = 0;
while(tmp[i] != '\0')
{
sum += tmp[i++];
}
return sum;
}
Logger* loggerCreate() {
// assume we'll store only 100 log messages
Logger * logger = malloc(sizeof *logger);
logger->log_array = calloc(0, LOGLIST * sizeof *logger->log_array);
return logger;
}
bool loggerShouldPrintMessage(Logger* obj, int timestamp, char * message) {
int hmsg = hash_message(message);
int hashval = hmsg % LOGLIST;
int * ht = &obj->log_array[hashval];
if(*ht + 10 <= timestamp || *ht == 0 )
{
*ht = timestamp;
return true;
}
return false;
}
void loggerFree(Logger* obj) {
free(obj->log_array);
free(obj);
}
int main()
{
Logger* obj = loggerCreate();
bool param_1 = loggerShouldPrintMessage(obj, 1, "foo");
bool param_2 = loggerShouldPrintMessage(obj, 2, "bar");
bool param_3 = loggerShouldPrintMessage(obj, 3, "foo");
bool param_4 = loggerShouldPrintMessage(obj, 8, "bar");
bool param_5 = loggerShouldPrintMessage(obj, 10, "foo");
bool param_6 = loggerShouldPrintMessage(obj, 11, "foo");
loggerFree(obj);
return 0;
}
If you dump that code, EXCEPT for the main into the following: https://leetcode.com/problems/logger-rate-limiter/
There is warning of shadow bytes, and heap buffer overflow. What I don't understand is that everything works fine when I run it through gdb, and when I compile it with gcc on my linux terminal. There is something I'm doing that is consistently wrong, because I always see this error.
Again, I know this is not exactly what this site is for, but I just would like to understand, and I can delete this question afterwards.