I am new in c and c++. I want to allocate memory and set a value to a pointer in my c++ file body so it will execute only once.
Here is my code:
myFile.h:
struct SelectedRows_t {
uint32_t rowsLen;
SelectData_t* rows[];
};
extern SelectedRows_t* selectedRows;
myFile.cpp
SelectedRows_t* selectedRows = (SelectedRows_t*)malloc(sizeof(selectedRows->rowsLen));
// some functions which use selectedRows variable
But I can't find a way to also initialize my variable. I need to set rowsLen
to 0 at the start of my program.
I don't have an init
or main
function as I am trying to write a library which can be used anywhere alongside other c++ codes.
I need to assign this 0 to this variable only once and in the start of my program.
I have to allocate memory to this variable myself because otherwise codes like this selectedRows->rowsLen
will crash my program.
I can't realloc
this variable in my functions because of rows
variable inside this struct needs to be free before any memory reallocation.
I don't know it this matters or not but I am writing this program to be run on esp32 boards.
Thanks in advance.