/*
* structs.h
*
* Created on: 14 Jun 2019
*/
#ifndef STRUCTS_H_
#define STRUCTS_H_
#include <stdint.h>
#include "line_mask.h"
#define CONFIG_LINE_NUM 80
#endif
/*
* line_mask.h
*
* Created on: 15 Nov 2019
*/
#ifndef LINE_MASK_H_
#define LINE_MASK_H_
#include "../Inc/structs.h"
typedef struct
{
#if CONFIG_LINE_NUM <= 64
uint64_t line_mask[1];
#elif (CONFIG_LINE_NUM <= 128)
uint64_t line_mask[2];
#elif (CONFIG_LINE_NUM <= 192)
uint64_t line_mask[3];
#elif (CONFIG_LINE_NUM <= 256)
uint64_t line_mask[4];
#else
#error "Unsupported CONFIG_LINE_NUM value"
#endif
} LineMask;
#endif
Hello. Could you explain something to me? I wrote code where header files refer to each other in order to use definitions. As a result, it seems that the file line_mask.h was compiled before structs.h (which was confirmed when I added a check for #ifdef CONFIG_LINE_NUM in the file line_mask.h). As a result, the variable CONFIG_LINE_NUM didn't exist at the time the structure LineMask was being defined.
Why, in this case, did the compiler not report an error but instead likely substituted 0 for this variable, creating a structure with the array line_mask[1]?
And a second question: After it created such a structure, during the initialization of a certain module, the structure was initialized with line_mask[1], but later in the program, in a different part, the structure was created with line_mask[2], leading to a memory leak during copying.
Why does the definition of the LineMask structure change after the CONFIG_LINE_NUM directive is declared, despite the #ifndef LINE_MASK_H_ condition in the header file?
CONFIG_LINE_NUMis set to 0. You can also instruct the compiler to treat all warnings as errors. You could also add a check using#ifndef CONFIG_LINE_NUMincludes.line_maskmember asuint64_t line_mask[(CONFIG_LINE_NUM + 63) / 64];and avoided all those preprocessor conditions. That would have resulted in an error ifCONFIG_LINE_NUMwas undefined.