• 0

C++ Char* buffer sizeof


Question

I am converting some code i wrote in C# to C++.

I have programmed a server which works perfectly fine HOWEVER,

When sending data to the client my:

unsigned short int buffer_size = 1024 *2; // 2mb
char *_buffer = new char[buffer_size]; //Char is prepared to send and recieve up to 2048 characters

The issue is that if the server sends the string:

"Neowin.net is awesome!" //length is 22

 

The char size is equal to the buffer_size which means i would be sending 2mb ever time i want to send, when all i really need is 22 bytes

 

 

FROM MY UNDERSTANDING i can not use:

sizeof(*buffer);

I would like to convert the char* to a string and get the length of the string and multiply it by the sizeof(char) to get the sizeof buffer

__

 

 

How can i convert a char * to a string?

 

 

 

Link to comment
https://www.neowin.net/forum/topic/1230937-c-char-buffer-sizeof/
Share on other sites

3 answers to this question

Recommended Posts

  • 0

What format is the string in the buffer in? Do you know if it is null terminated?
If so you can use strlen to get the length, or just pass the pointer to a std::string (make sure to free the buffer or ideally use std::unique_ptr).

std::unique_ptr<char[]> buffer = std::make_unique<char[]>(buffer_size);
 
// Either
const size_t length = strlen(buffer.get());
 
// Or
std::string str(buffer.get());
 

If the buffer is not null terminated, then you will need to supply the length to std::string yourself.

std::unique_ptr<char[]> buffer = std::make_unique<char[]>(buffer_size);
 
std::string str(buffer.get(), string_length);

You will also need to consider issues involving buffer overflow if the string is larger than your buffer size. There may also be more idiomatic C++ solutions using streams and string instead of raw buffers.

  • 0
  On 27/09/2014 at 11:21, Lant said:

What format is the string in the buffer in? Do you know if it is null terminated?

If so you can use strlen to get the length, or just pass the pointer to a std::string (make sure to free the buffer or ideally use std::unique_ptr).

std::unique_ptr<char[]> buffer = std::make_unique<char[]>(buffer_size);
 
// Either
const size_t length = strlen(buffer.get());
 
// Or
std::string str(buffer.get());
 

If the buffer is not null terminated, then you will need to supply the length to std::string yourself.

std::unique_ptr<char[]> buffer = std::make_unique<char[]>(buffer_size);
 
std::string str(buffer.get(), string_length);

You will also need to consider issues involving buffer overflow if the string is larger than your buffer size. There may also be more idiomatic C++ solutions using streams and string instead of raw buffers.

 

Though this format your provided surly is not recognizable to me (maybe its CLI?) Thank you! this got me on the right track.

I haven't written it yet but what i plan on doing is doing what i was going to from the start. I am going to get the String from the char * array and get the length of the string to multiply it by sizeof(char).

 

Thanks for reassuring my strategy. Do you mind telling me what version of C++ you provided?

  • 0

This is C++11 (well std::unique_ptr is, strlen is a C function available in C++ and std::string will definitely be available to you).

 

Why are you multiplying the length of the string by sizeof(char)? An important difference between C++ and C# is that char in C++ is typically 1 byte and 2 bytes in C#. This is because char is special to C# as it represents a character. In C++ char just represents a piece of memory that is at least 1 byte. So in C++ you have to consider how the string is going to be encoded (ASCII, UTF-8 will be the common two for one byte characters. LE UTF-16 is typical for 2 byte characters and I think is what Java and C# use). There is wchar_t for wide characters and char16_t and char32_t for 2 and 4 byte characters. Essentially strings in C++ get difficult when you want more than ASCII.

But that probably just confused you (it still confuses me and I've had to deal with big applications that mixes latin-1, UTF-8, ANSII and LE UTF-16).

This topic is now closed to further replies.
  • Posts

    • Good Bye and thanks for the fish
    • yeah i did some research and it seems they removed (or didn't carry over) the ability to change desktop icon fonts.....hopefully they bring it back for you soon...and yeah it also seems the ability to change the font has also been removed unless you adjust through registry or win aero.    if you haven't already, maybe trying to "adjust cleartype text" may help you. i tried running through it and noticed there were some bold font styles.   welcome (or bye haha) to windows 2025 btw!  
    • That'll be it for my 2018 Mac mini then. There is, of course, OpenCore Legacy Patcher . . .
    • Support cost cutting and nudging people to upgrade for profit. Pure and simple. Nothing as noble as hardware actually becoming "obsolete" about most of the hardware requirements from Apple, Microsoft, or Android companies either. iMac 2017 with AMD running legacy Core still have native GPU support in macOS 15 and run perfectly fine. Doesn't even need any fancy patching outside of the minimum patching needed to get macOS 15 on the system to bypass Apple system requirements aking to running windows 11 on a PC that doesn't match the arbitrary CPU generation requirements that make you bypass them too. You can usually tell if hardware is truly in the realm of "obsolete" if you are having a hard time finding a major Linux distro that'll install on it.
    • >Mozilla's Firefox has been left behind over time in terms of market share, as it has not been able to keep up with Chromium-based browsers in the performance department. I have no problems with Firefox's performance whatsoever. I suspect the reason Firefox is lagging in market share is that average consumers haven't heard of it and are fine with what was installed on their systems.
  • Recent Achievements

    • Conversation Starter
      Naomi723 earned a badge
      Conversation Starter
    • Week One Done
      abortretryfail earned a badge
      Week One Done
    • First Post
      Mr bot earned a badge
      First Post
    • First Post
      Bkl211 earned a badge
      First Post
    • One Year In
      Mido gaber earned a badge
      One Year In
  • Popular Contributors

    1. 1
      +primortal
      486
    2. 2
      +FloatingFatMan
      256
    3. 3
      snowy owl
      243
    4. 4
      ATLien_0
      222
    5. 5
      +Edouard
      191
  • Tell a friend

    Love Neowin? Tell a friend!