Buffer Size in C

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

For example, if the function I'm writing is as follows:

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

}

In this application, I will be given a string (ascii), and I will convert it to morse, using some other function to convert each ascii char to morse. The problem I am facing is to make sure that I have not exceeded the size of the buffer. I donโ€™t even know when to use the buffer size or how I reduce it every time I use it.

Of course, the output will be in morse (so I will add a line to morse, but I think I know how to do this, just the size of the buffer is something that I find difficult to understand)

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

+3
source share
6 answers

There seems to be some confusion regarding the "buffer". There is no buffer. morse-sizetells you how much memory has been allocated for morse(technically, the piece of memory that it points to morse). If the size of the fruit drink is 20, then you have 20 bytes. This is 19 bytes of usable space because strings end with a zero byte. You can think of morse-sizeas "maximum string length plus one."

morse-size, , morse, . morse - , , . , . , morse, . , morse, . C , .

, , " A3 5", . 6 , - A8.

, valgrind, , C .

C ? .

+3

.

int
ascii_to_morse(lookuptable *table,
               char* morse, int morse_size,
               char* ascii);

( strlen).

, , ascii ( , ) , morse, morse_size. ( ).

Edit: , , , , :

typedef void lookuptable; // we ignore this parameter below anyway
// but using void lets us compile the code

int
ascii_to_morse(lookuptable *table,
               char* morse, int morse_size,
               char* ascii)
{
  if (!ascii || !morse || morse_size < 1) { // check preconditions
    return 0; // and handle it as appropriate
    // you may wish to do something else if morse is null
    // such as calculate the needed size
  }
  int remaining_size = morse_size;
  while (*ascii) { // false when *ascii == '\0'
    char* mc_for_letter = ".-"; //BUG: wrong morse code value
    ++ascii;
    int len = strlen(mc_for_letter);
    if (remaining_size <= len) { // not enough room
      // 'or equal' because we must write a '\0' still
      break;
    }
    strcpy(morse, mc_for_letter);
    morse += len; // keep morse always pointing at the next location to write
    remaining_size -= len;
  }
  *morse = '\0';
  return morse_size - remaining_size;
}

// test the above function:
int main() {
  char buf[10];
  printf("%d \"%s\"\n", ascii_to_morse(0, buf, sizeof buf, "aaa"), buf);
  printf("%d \"%s\"\n", ascii_to_morse(0, buf, sizeof buf, "a"), buf);
  printf("%d \"%s\"\n", ascii_to_morse(0, buf, sizeof buf, "aaaaa"), buf);
  return 0;
}
+2
void ascii-morse (lookuptable *table, char* morse, char* ascii, int morse-size)

, .

ascii, , , morse : morse_size ( morse-size, , ) , .

:

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.

C , .

. , . , , '\0';

, , , - morse_size , morse . , morse_size , .

+2

. , - ( DEFINE ), ... ( , "", - , , ...)

, , (, ) .

+1

() - NULL .

0

, , , . , , .

char *ascii2morse(const char *ascii, lookuptable *table)

. , . , , ( * ascii) . , , .

realloc . , , . , , , .

UPS avoid traps where the user must first allocate an unknown amount of memory, and BOTH eliminate the unnecessary error condition "the user does not allocate sufficient memory."

If you really wanted to save memory, I would save every point / dash in the Morse code as 2 bits, not 8 bits. You have three words, a short and a long break. This is a minimum of 2 bits of space.

0
source

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


All Articles