How to find out how much memory to redistribute?

I have one question regarding the design of my application. Here's the pseudo code:

char* buffer_to_be_filled = (char*) malloc(somesize); fill_the_buffer(buffer_to_be_filled); free(buffer_to_be_filled); 

The problem is that I do not know how much fill_the_buffer size is fill_the_buffer .

I was thinking of a solution inside the fill_the_buffer function. I could perhaps redistribute the space inside when necessary; The problem is, is there a way to find out how much space I have available?

How is this usually solved? I think that the one who allocates the buffer should also redistribute the buffer, right?

NOTE. I fill the buffer with the fread function, so I don’t know how much space I need.

+4
source share
4 answers

Your function cannot realloc pointer that was passed to it, because realloc not guaranteed to return the same pointer that was passed (the new buffer may be too large to expand in place). A typical solution is that the function takes a second argument, which determines the size of the buffer, and returns an error code if the buffer is too small. Ideally, the error code will tell the user how big the buffer is, so he can redistribute it on his own and call the function. For example, from the man page for snprintf (which has this problem):

The functions snprintf () and vsnprintf () do not write more size bytes (including the terminating null byte ('\ 0')). If the result was truncated due to this limit, the return value is the number of characters (excluding the terminating null byte) that would be written to the final line if there was enough free space. Thus, a return value of size or larger means that the output has been truncated.

+3
source

You must pass the buffer size to the fill_the_buffer function. If the buffer is not large enough, your function should return an error value (f / e -1). If successful, your function can return the number of write bytes. This method is common practice for C.

+3
source

The fill_the_buffer() function fill_the_buffer() be in a much better position to know ...
- how to measure the buffer first and / or
- when to redistribute the buffer and by how much.

Therefore, it may be appropriate to change the API:

char * fill_the_buffer()
or maybe char * fill_the_buffer(size_t max_amount_caller_wants)

The caller for fill_the_buffer () will still be responsible for removing the buffer returned by the function, but the distribution and dimension will be left in the function logic.

This approach usually follows the idea of ​​leaving implementation details at a lower level, making higher levels more readable.

+2
source

I have a suggestion if you have no problems with allocating free memory:
Select the initial size at the beginning of the program using malloc , (try to make a good guess for this initial allocation), then in fill_the_buffer you may need to allocate more memory or you may not need all the allocated memory. In the first case, you can allocate the appropriate amount of memory (depending on your application and your available plunger) in several steps (for example, 10 MB for each leak), and then resume filling the buffer until you need more memory, and repeat this. while the buffer fills up, In the second case, you can simply realloc reduce the size of the allocated memory of your buffer.
But be careful using realloc specifically when you want to increase the size of the buffer, because it usually causes a lot of overhead (it has to find a large enough part of free memory, and then copy all the old data to the new part and free the old partition).

0
source

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


All Articles