How to get the length of a dynamically created array of structs in C?

前端 未结 4 960
轻奢々
轻奢々 2021-01-02 12:16

I am currently trying to get the length of a dynamically generated array. It is an array of structs:

typedef struct _my_data_ 
{
  unsigned int id;
  double          


        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-02 13:08

    sizeof cannot be used to find the amount of memory that you allocated dynamically using malloc. You should store this number separately, along with the pointer to the allocated chunk of memory. Moreover, you must update this number every time you use realloc or free.

提交回复
热议问题