Home > Enterprise >  Find out if the allocated memory was freed by the realloc() function in C
Find out if the allocated memory was freed by the realloc() function in C

Time:11-30

a part of my main contains

int* p = NULL;
p = malloc(sizof(int)*10); 
p = realloc(p, sizeof(int) * 5);
free(p);

Is there any way to find out if, after reallocation, the pointer p points to a memory block of size 20 bytes and not 40 bytes anymore?

The ideal would be to have a function that takes an adress of memory as argument and tells if it's allocated or free. Is there such a function?

Another idea would be to check the size before and after the realloc() of the allocated memory. But I don't see how sizeof() could help, because how would I identify the block of memory, sizeof() sends the size of variables and not the size of a block of memory. sizeof(p) will give me 8 bytes, since p is a pointer and sizeof(*p) is 4, since p points to an integer. Maybe there is a special use of sizeof() or some another function?

Read more if you want to know why I ask...
If I initialize my p to hold/point to an array

for (int i = 0; i < 3 ; i  ){
   p[i] = i;
}

I want now p to hold only {0,1,2} so I want to reallocate p from sizeof(int)* 5 to sizeof(int)*3. But let's say I don't really know if p should be reallocated, the memory block is 20 bytes, but maybe it's already 12 bytes, and realloc() is not needed. I know I can run realloc() either way and it won't hurt, so maybe it's not really a good reason for this question. But in a longer code it's easy to lose track of the amount of allocated memory.

Any help will be much appreciated.

PS If no one answers I will have to get satisfaction from valgrind sending 0 errors. After all, if something is wrong, for exemple writing in 21st, 22nd, 23rd and 24th bytes of memory (ex: p[4] = 7) of a block of 20 bytes (because p = realloc(p, sizeof(int) * 5)) valgrind sends errors of type "invalid write of size 4", but to get that I need to write in this memory. This method of verification makes me want to get errors, because if I can accurately predict an error then I know the acctual size of the allocated memory.

CodePudding user response:

There is no portable way of checking it.

You need to keep the size information yourself.

typedef struct String
{
    size_t size;
    int data[];
}data_type;


data_type *allocate(data_type *dt, size_t size)
{
    if(!dt || dt -> size != size)
    {
        dt = realloc(dt, sizeof(*dt)   size * sizeof(dt -> data[0]));
        if(dt)
        {
            dt -> size = size;
        }
    }
    return dt;
}

Usage is the same as realloc. If you pass NULL pointer it will allocate the the stricture and space for data. If you change the size, save the result in the temporary pointer and check if allocation did not fail. If the requested size is the same as the actual allocated space it will do nothing (as reallocation is not needed)

CodePudding user response:

Is there any way to find out if, after reallocation, the pointer p points to a memory block of size 20 bytes and not 40 bytes anymore?

No

This method of verification makes me want to get errors, because if I can accurately predict an error then I know the acctual size of the allocated memory.

Use -fsanitize=address for both compiler and linker. Read more here: https://stackoverflow.com/a/40215639/6699433

  • Related