Thread: Help with length of buffer

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    930

    Help with length of buffer

    For the third parameter of the recv() function MSDN says:

    "The length, in bytes, of the buffer pointed to by the buf parameter."

    Does it mean the size of the char[] variable or the length of the data in it?

    For example: "Hello world" is 12 characters long (with the '\0') which would be 1 byte because you can put 256 characters in one byte.

    If its the size of the variable its also enough 1 byte ( char[1] ) for the same reason i guess.

    Im a bit confused why everybody's using big numbers like 32, 64 etc...

    Code:
     
    char  buf[1];
    
    recv(s, buf, 1, 0);
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Does it mean the size of the char[] variable or the length of the data in it?

    The size of the buffer itself (the length of the text in the buffer doesn't really matter, anyway, since it's just going to be overwritten).

    >> For example: "Hello world" is 12 characters long (with the '\0') which would be 1 byte because you can put 256 characters in one byte.

    Not exactly. An 8 bit byte can hold *one* of 256 possible values. Or *eight* of 2 possible values (if you packed each value onto a single bit). You can do a better than that with certain compression schemes (such as arithmetic encoding), but that's beyond the scope of this post (see: information theory). The point is, using standard ASCII coding, 12 characters == 12 bytes.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Ducky View Post
    Im a bit confused why everybody's using big numbers like 32, 64 etc...
    Likely it's measured in bits.
    32 bits = 4 bytes.
    64 bits = 8 bytes.
    And so on. Common in the computer world.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Thank you Sebastiani and Elisia for your explanation!

    Have a good day!
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. Need help with a snake game (ncurses)
    By Adam4444 in forum C Programming
    Replies: 11
    Last Post: 01-17-2007, 03:41 PM
  3. Need help with program.
    By olgirl4life in forum C++ Programming
    Replies: 3
    Last Post: 12-11-2006, 11:06 PM
  4. writing a pack-style function, any advices?
    By isaac_s in forum C Programming
    Replies: 10
    Last Post: 07-08-2006, 08:09 PM
  5. buffer contents swapping
    By daluu in forum C++ Programming
    Replies: 7
    Last Post: 10-14-2004, 02:34 PM