• 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

    • Apple prices for sure suck, but performance, upgradability and flexibility are subjective to the use case and machine. For example, you will not find a mini PC in the same starting price range that has the same performance as a Mac Mini, nor will it be very upgradable, if at all at that size. Many cost-effective laptops are not upgradable past the storage. Inexpensive laptops are underperforming in comparison to more expensive models that might actually cost more than a MacBook . You won’t find battery life is comparable unless you go with Snapdragon, which limits compatibility and sacrifices upgradability. AIOs suck in both spaces, so I’ll not mention them. As for flexibility, I can do more in my use cases with a Mac than I can with a Windows or Linux machine. Where Apple really screws their users over is with upgrades, especially since you have to do them during purchase. The area where Apple can’t compete at all is desktop machines (not discussing AIOs). Outside of cost, it’s all subjective and situational.
    • Not with Proton, no. macOS isn’t Linux in any way, shape, or form.
    • AIDA64 was just showing the variable speed. Open Task Manager and go on the CPU tab and see the magic of your CPU speed going up and down as the PC does things in the background. C States Auto/Disabled actually forces all cores on (disabling the ability to park unneeded 3D Cache cores when they aren't needed), but does not have any effect on the CPU speed. I think setting the Power Plan in Windows to High Performance does boost the CPU clock higher, which also results in less energy efficiency with "everything turned up to 11" all the time when it isn't even needed. Older generation CPUs did not have the variable speeds we now have in modern CPUs, they operated on a fixed clock speed.
    • one drive saved my life .ive freed up some space i now have 227gigs.
    • "What the f is that name of that program?! Good thing I can find it in the easily accessible, and alphabetized list of apps." 🙂 I don't use the Start Menu a lot, but I'd be super annoyed if there wasn't some form of an app drawer in Windows.
  • Recent Achievements

    • One Month Later
      SekTheFirst earned a badge
      One Month Later
    • First Post
      zayanhani earned a badge
      First Post
    • First Post
      HarryTaylor earned a badge
      First Post
    • One Year In
      Eternal Tech earned a badge
      One Year In
    • One Month Later
      Eternal Tech earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      655
    2. 2
      ATLien_0
      237
    3. 3
      Michael Scrip
      231
    4. 4
      Steven P.
      141
    5. 5
      +FloatingFatMan
      137
  • Tell a friend

    Love Neowin? Tell a friend!