Help with length of buffer

This is a discussion on Help with length of buffer within the C++ Programming forums, part of the General Programming Boards category; For the third parameter of the recv() function MSDN says: "The length, in bytes, of the buffer pointed to by ...

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

    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);
    Compiler MSVC++ 2013 with Code::Blocks.

  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
    Posts
    22,920
    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
    812
    Thank you Sebastiani and Elisia for your explanation!

    Have a good day!
    Compiler MSVC++ 2013 with Code::Blocks.

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, 09:09 PM
  5. buffer contents swapping
    By daluu in forum C++ Programming
    Replies: 7
    Last Post: 10-14-2004, 03:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21