Why does the following code make my computer beep?

后端 未结 3 1111
天命终不由人
天命终不由人 2021-01-23 18:24

I\'m having a really hard time understanding why is this piece of code making my computer beep. I\'ve isolated this section of code to be the one producing the occasional beep,

相关标签:
3条回答
  • 2021-01-23 18:42

    The buffer probably contains a '\a' char which makes the computer beep. From 5.2.2 (Character display semantics) :

    Alphabetic escape sequences representing nongraphic characters in the execution character set are intended to produce actions on display devices as follows:

    • \a (alert) Produces an audible or visible alert without changing the active position.
    0 讨论(0)
  • 2021-01-23 18:42

    Agree with the '\a' beep explanation.

    One more point about your code:

    recvResult = recv(webSocket, buffer, BUFFER_LENGTH, 0);
    buffer[recvResult] = '\0';
    

    Note that recvResult will be -1 if there's an I/O error (or if you're working in the non-blocking mode and no data to read so far).

    In such a case you'll write into forbidden memory, which is (damn, how I hate this phrase) undefined behavior. Simply speaking - memory overwrite, which is bad.

    You should check for socket error before writing into buffer

    0 讨论(0)
  • 2021-01-23 18:56

    Nevermind, found it, it was actually the printf statement that was doing an occasionnal beep!

    0 讨论(0)
提交回复
热议问题