fix calloc/malloc

This commit is contained in:
Dennis Eichhorn 2022-09-20 17:24:17 +02:00
parent 7f3fc9a99f
commit 641ff4e4e2
8 changed files with 29 additions and 27 deletions

View File

@ -27,7 +27,7 @@ namespace Image {
cv::Size dim = in.size(); cv::Size dim = in.size();
cv::Mat out(dim, in.type()); cv::Mat out(dim, in.type());
float *intImg = malloc(dim.width * dim.height * sizeof(float)); float *intImg = (float *) malloc(dim.width * dim.height * sizeof(float));
float sum; float sum;
cv::Vec3b bgr; cv::Vec3b bgr;

View File

@ -50,7 +50,7 @@ namespace Stdlib {
ht *create_table(void) ht *create_table(void)
{ {
ht *table = malloc(sizeof(ht)); ht *table = (ht *) malloc(sizeof(ht));
if (table == NULL) { if (table == NULL) {
return NULL; return NULL;
} }
@ -58,7 +58,7 @@ namespace Stdlib {
table->size = 0; table->size = 0;
table->max = 16; table->max = 16;
table->entries = calloc(table->max, sizeof(entry)); table->entries = (entry *) calloc(table->max, sizeof(entry));
if (table->entries == NULL) { if (table->entries == NULL) {
free(table); free(table);
return NULL; return NULL;
@ -89,7 +89,7 @@ namespace Stdlib {
const char *_set_entry(entry *entries, size_t max, const char *key, void *value, size_t *size) const char *_set_entry(entry *entries, size_t max, const char *key, void *value, size_t *size)
{ {
unsigned long long hash = hash_key(key); unsigned long long hash = hash_key(key);
size_t index = (size_t)(hash & (unsigned long long)(max - 1)); size_t index = (size_t)(hash & (unsigned long long)(max - 1));
while (entries[index].key != NULL) { while (entries[index].key != NULL) {
if (strcmp(key, entries[index].key) == 0) { if (strcmp(key, entries[index].key) == 0) {
@ -107,7 +107,7 @@ namespace Stdlib {
if (size != NULL) { if (size != NULL) {
key = strdup(key); key = strdup(key);
if (key == NULL) { if (key == NULL) {
return NULL return NULL;
} }
++(*size); ++(*size);
@ -126,7 +126,7 @@ namespace Stdlib {
return false; return false;
} }
entry *new_entries = calloc(new_max, sizeof(entry)); entry *new_entries = (entry *) calloc(new_max, sizeof(entry));
if (new_entries == NULL) { if (new_entries == NULL) {
return false; return false;
} }
@ -140,7 +140,7 @@ namespace Stdlib {
free(table->entries); free(table->entries);
table->entries = new_entries; table->entries = new_entries;
table->max = new_max; table->max = new_max;
return true; return true;
} }
@ -178,7 +178,7 @@ namespace Stdlib {
if (table->entries[i].key != NULL) { if (table->entries[i].key != NULL) {
entry tmp = table->entries[i]; entry tmp = table->entries[i];
it->key = tmp.key; it->key = tmp.key;
it->value = tmp.value; it->value = tmp.value;
return true; return true;
@ -196,6 +196,8 @@ namespace Stdlib {
free(table->entries); free(table->entries);
free(table); free(table);
table = NULL;
} }
}; };
} }

View File

@ -46,10 +46,10 @@ namespace Utils {
} }
inline inline
void reverse_char(char *arr, int size) void reverse_char(char **arr, int size)
{ {
for (int low = 0, high = size - 1; low < high; ++low, --high) { for (int low = 0, high = size - 1; low < high; ++low, --high) {
char tmp = arr[low]; char* tmp = arr[low];
arr[low] = arr[high]; arr[low] = arr[high];
arr[high] = tmp; arr[high] = tmp;
} }

View File

@ -27,7 +27,7 @@ namespace Utils {
for (int i = 0; i < length - 1; ++i) { for (int i = 0; i < length - 1; ++i) {
if (strcmp(id, argv[i]) == 0) { if (strcmp(id, argv[i]) == 0) {
return argv[i + 1]; return i + 1 >= length ? NULL : argv[i + 1];
} }
} }

View File

@ -95,14 +95,14 @@ namespace Utils {
FILE *fp = fopen(filename, "rb"); FILE *fp = fopen(filename, "rb");
if (!fp) { if (!fp) {
return NULL; return file;
} }
fseek(fp, 0, SEEK_END); fseek(fp, 0, SEEK_END);
file.size = ftell(fp); file.size = ftell(fp);
fseek(fp, 0, SEEK_SET); fseek(fp, 0, SEEK_SET);
file.content = malloc(file.size); file.content = (char *) malloc(file.size);
if (!file.content) { if (!file.content) {
fprintf(stderr, "CRITICAL: malloc failed"); fprintf(stderr, "CRITICAL: malloc failed");
} }

View File

@ -20,12 +20,12 @@ namespace Utils {
inline inline
char* generate_string( char* generate_string(
size_t min = 10, size_t max = 10, size_t min = 10, size_t max = 10,
char* charset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", int charsetLength = 62 char *charset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", int charsetLength = 62
) { ) {
srand(time(0)); srand(time(0));
size_t length = (rand() % (max - min + 1)) + min; size_t length = (rand() % (max - min + 1)) + min;
char* randomString = malloc(length + 1); char *randomString = (char *) malloc(length + 1);
for (size_t i = 0; i < length; ++i) { for (size_t i = 0; i < length; ++i) {
randomString[i] = charset[rand() % charsetLength]; randomString[i] = charset[rand() % charsetLength];

View File

@ -40,10 +40,10 @@ namespace Utils {
text_diff computeLCSDiff (char **from, int fromSize, char **to, int toSize) text_diff computeLCSDiff (char **from, int fromSize, char **to, int toSize)
{ {
char **diffValues = malloc(fromSize * toSize * sizeof(char *)); char **diffValues = (char **) malloc(fromSize * toSize * sizeof(char *));
int *diffMasks = calloc(fromSize * toSize, sizeof(int)); int *diffMasks = (int *) calloc(fromSize * toSize, sizeof(int));
int *dm = calloc((fromSize + 1) * (toSize + 1), sizeof(int)); int *dm = (int *) calloc((fromSize + 1) * (toSize + 1), sizeof(int));
if (!diffValues || !diffMasks || !dm) { if (!diffValues || !diffMasks || !dm) {
fprintf(stderr, "CRITICAL: malloc failed"); fprintf(stderr, "CRITICAL: malloc failed");
@ -77,7 +77,7 @@ namespace Utils {
while (i > 0 || j > 0) { while (i > 0 || j > 0) {
if (j > 0 && dm[i * fromSize + (j - 1)] == dm[i * fromSize + j]) { if (j > 0 && dm[i * fromSize + (j - 1)] == dm[i * fromSize + j]) {
diffValues[diffIndex] = malloc((strlen(to[j - 1]) + 1) * sizeof(char)); diffValues[diffIndex] = (char *) malloc((strlen(to[j - 1]) + 1) * sizeof(char));
if (!diffValues[diffIndex]) { if (!diffValues[diffIndex]) {
fprintf(stderr, "CRITICAL: malloc failed"); fprintf(stderr, "CRITICAL: malloc failed");
} }
@ -92,7 +92,7 @@ namespace Utils {
} }
if (i > 0 && dm[(i - 1) * fromSize + j] == dm[i * fromSize + j]) { if (i > 0 && dm[(i - 1) * fromSize + j] == dm[i * fromSize + j]) {
diffValues[diffIndex] = malloc((strlen(from[i - 1]) + 1) * sizeof(char)); diffValues[diffIndex] = (char *) malloc((strlen(from[i - 1]) + 1) * sizeof(char));
if (!diffValues[diffIndex]) { if (!diffValues[diffIndex]) {
fprintf(stderr, "CRITICAL: malloc failed"); fprintf(stderr, "CRITICAL: malloc failed");
} }
@ -106,7 +106,7 @@ namespace Utils {
continue continue
} }
diffValues[diffIndex] = malloc((strlen(from[i - 1]) + 1) * sizeof(char)); diffValues[diffIndex] = (char *) malloc((strlen(from[i - 1]) + 1) * sizeof(char));
if (!diffValues[diffIndex]) { if (!diffValues[diffIndex]) {
fprintf(stderr, "CRITICAL: malloc failed"); fprintf(stderr, "CRITICAL: malloc failed");
} }
@ -124,8 +124,8 @@ namespace Utils {
free(dm); free(dm);
diffValues = realloc(diffValues, diffIndex * sizeof(char *)); diffValues = (char **) realloc(diffValues, diffIndex * sizeof(char *));
diffMasks = realloc(diffMasks, diffIndex * sizeof(int)); diffMasks = (int *) realloc(diffMasks, diffIndex * sizeof(int));
if (!diffValues || !diffMasks) { if (!diffValues || !diffMasks) {
fprintf(stderr, "CRITICAL: malloc failed"); fprintf(stderr, "CRITICAL: malloc failed");

View File

@ -15,22 +15,22 @@
#ifdef _MSC_VER #ifdef _MSC_VER
#define ASSERT_EQUALS(a, b, t1, t2) (\ #define ASSERT_EQUALS(a, b, t1, t2) (\
if (a == b) { \ if ((a) == (b)) { \
printf("."); \ printf("."); \
} else { \ } else { \
printf("[F]"); \ printf("[F]"); \
printf("\n\n%s - %i: ", __FILE__, __LINE__); \ printf("\n\n%s - %i: ", __FILE__, __LINE__); \
printf(t1, a); printf(" != "); printf(t2, b); printf("\n"); \ printf((t1), (a)); printf(" != "); printf((t2), (b)); printf("\n"); \
return 0; } \ return 0; } \
) )
#define ASSERT_EQUALS_WITH_DELTA(a, b, delta, t1, t2) (\ #define ASSERT_EQUALS_WITH_DELTA(a, b, delta, t1, t2) (\
if (oms_abs(a - b) <= _delta) { \ if (oms_abs((a) - (b)) <= (delta)) { \
printf("."); \ printf("."); \
} else { \ } else { \
printf("[F]"); \ printf("[F]"); \
printf("\n\n%s - %i: ", __FILE__, __LINE__); \ printf("\n\n%s - %i: ", __FILE__, __LINE__); \
printf(t1, a); printf(" != "); printf(t2, b); printf("\n"); \ printf((t1), (a)); printf(" != "); printf((t2), (b)); printf("\n"); \
return 0; } \ return 0; } \
) )
#else #else