Buffer size in C

18,088

Solution 1

It sounds like there's some confusion about the "buffer". There is no buffer. morse-size is telling you how much memory has been allocated to morse (technically, the chunk of memory that morse points to). If morse-size is 20 then you have 20 bytes. This is 19 bytes of usable space, because strings are terminated by a null byte. You can think of morse-size as "maximum length of the string plus one".

You need to check morse-size to make sure you're not writing more bytes into morse than it can hold. morse is nothing more than a number pointing to a single spot in memory. Not a range, but a single spot. What's been allocated to morse comes after that. If you put more than that into morse you risk overwriting someone else's memory. C will NOT check this for you, this is the price of maximum performance.

Its like if you went to a theater and the usher tells you, "you can have seat A3 and the next 5" and then leaves. You have to be polite and not take 6 seats, somebody else was given A8.

Tools such as valgrind are invaluable to spot memory mistakes in C and keep your sanity.

Aren't strings in C a hoot? Welcome to the single largest root cause of bugs in the entire computing world.

Solution 2

void ascii-morse (lookuptable *table, char* morse, char* ascii, int morse-size)

You have the size of the output buffer already passed in, by the looks of that prototype above.

ascii will no doubt be a null terminated string and morse will be the output buffer: morse_size (not morse-size as you have it, since that's not a valid identifier) will be how many characters you are allowed to write.

The pseudocode will be something like:

set apointer to start of ascii, mpointer to start of morse.
while apointer not at end of ascii:
    get translation from lookuptable, using the character at apointer.
    if length of translation is greater than morse_size:
        return an error.
    store translation to mpointer.
    add 1 to apointer.
    add length of translation to mpointer.
    subtract length of translation from morse_size.
if morse_size is zero:
    return an error.
store string terminator to mpointer.

You'll have to convert that to C and implement the lookup function but that should be a good start.

The pointers are used to extract from, and insert into, the relevant strings. For every character, you basically check whether there is enough room left in the output buffer for adding the morse code segment. And, at the end, you also need to check there's enough room for the string terminator character '\0';

The way in which you check if there is enough room is by reducing the morse_size variable by the length of the string you're adding to morse each time through the loop. That way, morse_size will always be the size remaining in the buffer for your use.

Solution 3

The buffer size cannot be inferred from the pointer alone. It needs to either be passed as an argument, or be somehow know (as from DEFINE values or other constants) or implicitly known... (this latter, implicit approach is "dangerous" for if the size is somehow changed but such changes are not reflected in places where the buffer is used...)

Alternatively, and more typically in the the case of input buffers (buffers which the function will read from), the end of the buffer may be marked by a special character or a sequence of such charcters.

Share:
18,088
c2009l123
Author by

c2009l123

Updated on June 04, 2022

Comments

  • c2009l123
    c2009l123 about 2 years

    When provided with a buffer size in C, how do I know how much is left and when do I need to stop using the memory?

    For example, if the function I am writing is this:

    void ascii_morse (lookuptable *table, char* morse, char* ascii, int morse_size) {
    
    }
    

    In this application I will be passed a string (ascii) and I will convert it to morse using some other function to convert each ascii char to morse. The problem I'm facing is how to make sure I am not exceeding the buffer size. I don't even know when to use the buffer size or how I decrease it everytime I use it.

    Of course the output will be to morse (so i will be adding string to morse, but I guess I know how to do that, it is just the buffer size is what is hard to understand to me)

    If you need any more information to understand the problem please tell me, I tried my best to explain it.