Passing pointer to first string element as buffer

bartop

I have a bunch of old linux code which does something like this:

int size_of_buffer = /*stuff computed dynamically*/;
char *buffer = malloc(size_of_buffer);
recv(socket, buffer, size_of_buffer, 0);
//do some processing of the buffer as string
free(buffer);

When I was migrating it to C++ I changed it like this:

int size_of_buffer = /*stuff computed dynamically*/;
const auto buffer = make_unique<char[]>(size_of_buffer);
recv(socket, buffer.get(), size_of_buffer, 0);
const std::string str_buffer = buffer.get();
//do some processing on str_buffer

Which you can't fail to notice causes double memory allocation and potentially multiple copying of data. My idea now is to pass the pointer to first character of the std::string with reserved storage, like this:

int size_of_buffer = /*stuff computed dynamically*/;
std::string buffer;
buffer.reserve(size_of_buffer);
recv(socket, &(buffer[0]), size_of_buffer, 0);
//do some processing on buffer

Is above code safe and well defined or there are some caveats and dangers that need to be avoided?

n314159

A similar question was asked here. The short answer is: it is not possible without copying.

Below C++17, there is no non-const overload of std::string::data(), and

1) Modifying the character array accessed through the const overload of data has undefined behavior.

Hence, you cannot modify the string through data.

Since C++11,

data() + i == std::addressof(operator[](i)) for every i in [0, size()].

Therefore, you also cannot modify the string through &(buffer[0]).

Before C+11, it is actually not very clear to me, what exactly is allowed, so maybe modifying through &(buffer.begin()) is ok, but I don't think so.

On cppreference, there is actually a quote that confounds me a bit

The elements of a basic_string are stored contiguously, that is, for a basic_string s, &*(s.begin() + n) == &*s.begin() + n for any n in [0, s.size()), or, equivalently, a pointer to s[0] can be passed to functions that expect a pointer to the first element of a (null-terminated (since C++11)) CharT[] array.

I think this means to const array, since otherwise it would not fit to the rest of the documentation and right now I do not have the time to go through the standard.

Since C++17, your code is ok, if you use std::string::resize instead of reserve, since data() only guarantees a valid range on [data(), data() + size()) (or you can just construct the string with the right size). There is no-non-copy way to create a string from a char *.

You can use a std::string_view, which has a constant constructor from char *. It does exactly what you want here, since it has no ownership on the pointer and memory.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python ctypes passing string pointer / buffer

From Dev

Passing the pointer to the first element of a vector if any

From Dev

get address of pointer to first element of array and copy all elements to new buffer

From Dev

Is a temporary variable needed when just passing the pointer to the first element of a linked list?

From Dev

Why does printing the first element of a pointer to pointer to a char print the contents of a string?

From Dev

Passing destination string as a pointer in strcpy

From

Passing pointer to string array in Golang

From Dev

Passing String Pointer Array in C

From Dev

Passing a pointer (string) to a C function

From Dev

Passing default string pointer to function

From Dev

Getting a Pointer to the First Element of a Struct

From Dev

passing String buffer into array into List in android

From Dev

Casting problems with passing a struct by reference instead of a pointer to a buffer

From Dev

How to put a large string pointed to by a pointer to a buffer

From Dev

Passing in an element in an array as a pointer in a function in C

From

Cgo pointer passing rule for pointer string slice (*[]string)?

From Dev

Passing string to function which accepts pointer to char

From Dev

Delphi - Passing variable pointer as string to another function

From Dev

Passing the address of a dereferenced pointer to a string offset

From Dev

Passing a C string and using it with a local pointer

From Dev

string literal pointer issue passing to nested functions

From Dev

Getting "first argument must be a string or Buffer" error

From Dev

MongoDB: first argument must be string or Buffer?

From Dev

A pathological way of passing an array: by reference to the first element

From Dev

What is the best way to copy a buffer and return a pointer to the next available element?

From Dev

Why sizes of an array and a pointer to a first element are different?

From Dev

Pointer to one before first element of array

From Dev

Access first element of pointer to vector of object pointers?

From Dev

First element in a double pointer to struct is jiberrish

Related Related

  1. 1

    Python ctypes passing string pointer / buffer

  2. 2

    Passing the pointer to the first element of a vector if any

  3. 3

    get address of pointer to first element of array and copy all elements to new buffer

  4. 4

    Is a temporary variable needed when just passing the pointer to the first element of a linked list?

  5. 5

    Why does printing the first element of a pointer to pointer to a char print the contents of a string?

  6. 6

    Passing destination string as a pointer in strcpy

  7. 7

    Passing pointer to string array in Golang

  8. 8

    Passing String Pointer Array in C

  9. 9

    Passing a pointer (string) to a C function

  10. 10

    Passing default string pointer to function

  11. 11

    Getting a Pointer to the First Element of a Struct

  12. 12

    passing String buffer into array into List in android

  13. 13

    Casting problems with passing a struct by reference instead of a pointer to a buffer

  14. 14

    How to put a large string pointed to by a pointer to a buffer

  15. 15

    Passing in an element in an array as a pointer in a function in C

  16. 16

    Cgo pointer passing rule for pointer string slice (*[]string)?

  17. 17

    Passing string to function which accepts pointer to char

  18. 18

    Delphi - Passing variable pointer as string to another function

  19. 19

    Passing the address of a dereferenced pointer to a string offset

  20. 20

    Passing a C string and using it with a local pointer

  21. 21

    string literal pointer issue passing to nested functions

  22. 22

    Getting "first argument must be a string or Buffer" error

  23. 23

    MongoDB: first argument must be string or Buffer?

  24. 24

    A pathological way of passing an array: by reference to the first element

  25. 25

    What is the best way to copy a buffer and return a pointer to the next available element?

  26. 26

    Why sizes of an array and a pointer to a first element are different?

  27. 27

    Pointer to one before first element of array

  28. 28

    Access first element of pointer to vector of object pointers?

  29. 29

    First element in a double pointer to struct is jiberrish

HotTag

Archive