-2

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.

kaylum
  • 13,833
  • 2
  • 22
  • 31
Pouria Moosavi
  • 662
  • 7
  • 22
  • 1
    You can't do function calls outside of any function in c or c++ that way. C++ constructors may be executed before `main()` though. You might be interested in reading about the _Singleton pattern_. – πάντα ῥεῖ May 09 '21 at 11:50
  • Read some books from [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), all of them should introduce the basic concept of constructors. – KamilCuk May 09 '21 at 11:52
  • `malloc` works now. I have memory allocated to my variable but it's value is not 0. So the problem is not the function call. is to set the value. – Pouria Moosavi May 09 '21 at 11:53
  • @KamilCuk I am not inside a class. I don't know how to use constructors outside of a class. Is there even valid to do this? I just have a cpp file no class. – Pouria Moosavi May 09 '21 at 11:58
  • `I don't know how to` then buy one of the books and learn. `I just have a cpp file no class` Then implement the "class" in a "cpp file" and call it's constructors and initialize the data in it. – KamilCuk May 09 '21 at 12:04
  • @KamilCuk I just can't understand why I can declare and initialize variable in cpp file but I cant set value for a variable which is declared in previous line. And I think your overall answer is that you don't know but I have to read more books and recreate my whole file. – Pouria Moosavi May 09 '21 at 12:12

1 Answers1

0

I solved the problem by using a function to do all my works in it and return the pointer to the final SelectedRows_t:

SelectedRows_t* createSelectedRows() {
  SelectedRows_t* selectedRowsTemp = (SelectedRows_t*)malloc(sizeof(selectedRows->rowsLen));
  selectedRowsTemp->rowsLen=0;
  return selectedRowsTemp;
}

SelectedRows_t* selectedRows = createSelectedRows();
Pouria Moosavi
  • 662
  • 7
  • 22