3

I have an algorithm that requires making two passes a file's data. The file may be stdin or stream (like a |) since this is a command line tool, which makes me unfortunately (to the best of my knowledge) rule out mmap.

I require the information from the 1st pass in order to perform a write operation on the 2nd pass. This is because I need a sum of all the bytes on the first pass for a specific cipher on the second pass.

One way I have thought of to do this is to use the heap as a single contiguous region of memory, and to allocate additional size once the end has been reached with sbrk (similar to what I believe the first implementation of bash) did. Is there a simple way to do this?

Specifically, how can I avoid stdlib's set-up of the heap and do so myself?

asked Jun 12, 2023 at 8:32
10
  • You probably either need to read the whole input into malloced memory, or you need copy it into a temporary file. OTOH, once you have copied the input into a file, you can use mmap on that file. Commented Jun 12, 2023 at 8:37
  • 2
    Read into a buffer, doing all your first-pass processing while doing that. Then use the buffer for your second pass. If you read from stdin (no matter if it's from a terminal or a pipe) that's the only way to do multiple passes over the input, because once the data is read you can't seek back. If the buffer is in memory or as a temporary file doesn't matter, but you need some kind of buffer to store the input. Commented Jun 12, 2023 at 8:40
  • 1
    Two-pass problems are rare. What's yours? Entire compilers can be written in one pass. Commented Jun 12, 2023 at 8:46
  • 1
    @user129393192, "Do I simply have to allocate memory for the entire length of the data as I read, or is there an alternative?" --> Yes. Post details of your task to find useful alternatives. Commented Jun 12, 2023 at 10:36
  • Also please note that whenever "efficiency" is mentioned, its more often than not a red herring. Gather requirements, do analysis of them, and do a good design from the analysis. Then concentrate to write a good, simple, maintainable program from the design. If there are "efficiency" requirements, they should already be in the analysis and the design, and the code should reflect that. Commented Jun 12, 2023 at 10:56

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.