0

I can't for the life of me figure out how to solve this in c.

I have the following structs:

typedef struct
{
    uint8_t         index = 0;
    DLPageItem_t    *pageItems = NULL;
    uint8_t         pageItemCount = 0;
    uint8_t         selectedItemIndex = 0;
} DLPage_t;

typedef struct
{
    uint8_t         index = 0;
    void            *valuePtr = NULL;
    uint16_t        tmpValue = 0;
    DLItemType_t    type = DLITEMTYPE::NOTYPE;
    uint8_t         row = 0;
    uint8_t         col = 0;
    uint8_t         targetPageId = 0;
    DLItemAction_t  action = DLITEMACTION::EDIT;
    bool            selectable = false;
    bool            editing = false;
} DLPageItem_t;

And I want to have a dynamic array of DLPage_t (with realloc) and then be able to anytime add DLPageItem_t to any of the already created DLPage_t array members.

So I tried to have a

DLPage_t *_pages = NULL;

and do

this->_pageCount++;    
this->_pages = (DLPage_t*)realloc(this->_pages, this->_pageCount * sizeof(DLPage_t));

Then I'm accessing the "pages" and setting the values like this:

this->_pages[this->_pageCount - 1].index = this->_pageCount - 1;

The function which adds DLPageItem_t structs to a DLPage_t struct member, I do it like this:

this->_pages[pageId].pageItemCount++;
    this->_pages[pageId].pageItems = (DLPageItem_t*)realloc(this->_pages[pageId].pageItems, this->_pages[pageId].pageItemCount * sizeof(DLPageItem_t));

I have some very funny business going on so I guess I'm doing something wrong and accessing/overwriting memory where I shouldn't.

Can someone please verify whether what I'm doing is correct? I tried to apply/adapt this to my issue but I just can't figure it out: https://stackoverflow.com/a/15397992

Thanks!

azzurro
  • 1
  • 2
  • 2
    `c/c++.` -- There is no such language. Since you tagged this as `C++`, use `std::vector` and leave `realloc` alone. – PaulMcKenzie Oct 28 '21 at 00:27
  • c++ because i use classes. i am on avr with arduino framework (there you have it) and don't have vectors available. – azzurro Oct 28 '21 at 00:35
  • Post your code compile-able here. Thanks! – chux - Reinstate Monica Oct 28 '21 at 00:37
  • If you can't use `std::vector` then I suggest writing your own vector class to isolate the array logic, and then use that class wherever you need a dynamic array. – Remy Lebeau Oct 28 '21 at 00:40
  • realloc doesn't have to return a valid pointer are you checking for those cases? – Moshe Rabaev Oct 28 '21 at 00:45
  • moshe rabaev yes i am checking that. chux will do tomorrow! remy lebeau that's a bit overkill and memory consuming for the application, i am afraid. shouldn't this be doable with my approach? forgot to mention that i only add items to the arrays at the beginning of the program but i never delete any of them. so deleting items is not a requirement. – azzurro Oct 28 '21 at 00:48
  • There is no `this` pointer in C. If this is C++ then why did you remove the C++ tag but left the C tag? It's still perfectly unclear which language you are asking about and answers will be very different depending on language. – Lundin Oct 28 '21 at 07:52
  • Also, contrary to popular belief, Arduino is a horrible platform for learning programming (and for everything else too). Notably, you can forget all about heap allocation on microcontroller systems. – Lundin Oct 28 '21 at 07:54
  • oh my god my question was specificly how to solve this with arrays of structs and how to properly mallocing/reallocing the space within those arrays for the struct arrays. i can't use some c++ stuff like list or vector so I have to stick to structs and arrays. classes are available, obviously, as i have clearly been using them. so how would i make an array of a struct which i can dynamically expand, which each of the array members contains another array of another struct, which is dynamically expandable as well? – azzurro Oct 28 '21 at 10:34

0 Answers0