Tracking how much malloc memory is allocated

After a quick look at related SO questions, I came to the conclusion that there is no function that checks the amount of memory that malloc assigned to the pointer. I am trying to replicate some of the main functions of std :: string (mainly of dynamic size) with a simple char * in C and do not want to constantly call realloc. I think I will need to track how much memory has been allocated. To do this, I am considering creating a typedef that will contain the string itself and an integer with the amount of allocated memory, something like this:

typedef struct {
    char * str;
    int mem;
} my_string_t;

Is this the best solution, or maybe you can offer something that will bring the best results? Thanks in advance for your help.

+3
source share
6 answers

You want to allocate space for both length and string in the same memory block. It may be what you intended with your structure, but you have reserved space only for a pointer to a string.

A space containing string characters should be allocated.

For instance:

typedef struct
{
    int num_chars;
    char string [];
} my_string_t;

my_string_t * alloc_my_string (char * src)
{
    my_string_t * p = NULL;
    int N_chars = strlen (src) + 1;

    p = malloc (N_chars + sizeof (my_string_t));
    if (p)
    {
         p-> num_chars = N_chars;
         strcpy (p-> string, src);
    }
    return p;
}

In my example, to access a pointer to your string, you access a stringmember my_string_t:

my_string_t * p = alloc_my_string("hello free store.");
printf("String of %d bytes is '%s'\n", p->num_chars, p->string);

, , , . , , , .

:

+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
| 00 | 00 | 00 | 11 | 'h'| 'e'| 'l'| 'l'| 'o'| 20 | 'f'| 'r'| 'e'| 'e'| 20 | 's'| 't'| 'o'| 'r'| 'e'| '.'| 00 |
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
^^                   ^
||                   |
p|                   |
 p->num_chars        p->string

, p->string , , ( 32-, ) .

, C :

typedef struct
{
    int num_chars;
    char string[0];
} my_string_t;

, , C99.

:

typedef struct
{
    int num_chars;
} mystr2;

char * str_of_mystr2(mystr2 * ms)
{
    return (char *)(ms + 1);
}

mystr2 * alloc_mystr2(char *src)
{
    mystr2* p = NULL;
    size_t N_chars = strlen(src) + 1;

    if (N_chars num_chars = (int)N_chars;
         strcpy(str_of_mystr2(p), src);
    }
    return p;
} 

printf("String of %d bytes is '%s'\n", p->num_chars, str_of_mystr2 (p));

, p->string, str_of_mystr2(). , , , .

size_t, , . , INT_MAX, . int, assert(p->num_chars >= 0); -. , , assert(p->num_chars < UINT_MAX / 2); , , .

, , , UINT_MAX/2, .

+5

. , , , . strlen() , ++ std::string.

+2

, . , malloc - , .

+1

malloc ( realloc)
.

+1

. malloc, .

As an example, consider "writing solid code"

+1
source

I think you could use malloc_usable_size .

+1
source

Source: https://habr.com/ru/post/1727820/


All Articles