Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are different tricks to handle random length data and all of them need dynamically allocated memory. </p> <p>The simplest way to provide it in Linux is to use <strong>sys_brk</strong> function, but it allows only one memory block to be allocated. </p> <p>There are libraries that provide heap management. One such library, entirely in assembly language is <a href="http://fresh.flatassembler.net/index.cgi?page=content/articles/2_FreshLibDoc.txt#H12.1." rel="nofollow"><strong>FreshLib</strong></a>. Another option is to link with the C standard library.</p> <p>Then, there are two cases to read the data in the dynamically allocated buffer, depending on whether or not you know the data length in advance (in run time).</p> <h2>Known data size</h2> <p>It is simple - allocate the buffer with the needed size and read the data entirely.</p> <h2>Unknown data size - so called stream data</h2> <h3>Read and then copy</h3> <p>The only possible way to read stream data in the memory is to read a fixed chunks of this data and then to copy it into the dynamically allocated buffer. When the buffer is filled up, you need to reallocate it with bigger size and then to continue until the all data is read.</p> <p>Note, that the memory reallocation is expensive operation, so it is better to allocate more than needed memory. Common algorithm is to double the size of the allocated memory on every reallocation. Personally I think this strategy is too aggressive and often use multiplying the size by 1.5;</p> <h3>Don't read the whole data at all</h3> <p>It is often possible to not read the whole data in the memory, but to process it on the fly as it is read in small fixed chunks. This method needs a little bit more complex algorithms, but has the big advantage to use very small memory, not need dynamically allocated memory and avoids the copy of the data between multiply memory locations.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload