• 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
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.

Link to comment
Share on other sites

  • 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.

 

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?

Link to comment
Share on other sites

  • 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).

Link to comment
Share on other sites

This topic is now closed to further replies.