How do you return a char * from two char pointer? Error when returning local pointer. What could be the best procedures to deal with RAW data buffer

huangapple go评论127阅读模式
英文:

How do you return a char * from two char pointer? Error when returning local pointer. What could be the best procedures to deal with RAW data buffer

问题

Sure, here's the translated content:

假设我有一个prepend函数:

  1. // 例如 xMessage = "apple";
  2. // 例如 yMessage = "juice";
  3. // 答案应该是 newMessage = "applejuice";
  4. char* concat(char *xMessage, int xSize, char* yMessage, int ySize){
  5. char newMessage[xSize+ySize];
  6. for(....){
  7. // 复制xMessage
  8. }
  9. for(....){
  10. // 复制yMessage
  11. }
  12. return (char *) newMessage;
  13. }

我知道我可以使用字符串,但最好将它们保留为字节数组。为了避免返回本地指针的错误,最好的解决方案是什么?

解决方案:根据原始数据的类型处理/算法的需求,创建一个uint8_t的向量或deque。我最终使用了std::vector<uint8_t>。例如:

  1. std::vector<uint8_t> data(buffer + bytesIn);

因为在我的情况下,我从套接字接收数据。

编辑:在阅读回复后,最方便的解决方案是创建一个char的向量或字符串。由于我有大量字节要发送,我决定使用std::vector<uint8_t>,因为在程序的后续处理中需要进行更多操作。感谢大家让我意识到这一点,并帮助我解决了这个问题。我之所以使用原始char指针是因为我处理的数据是原始数据,但将其转换为向量后,更容易操作数据,而不是处理char指针。

英文:

Lets say I have a prepend function:

  1. //for example xMessage = &quot;apple&quot;;
  2. //for example yMessage = &quot;juice&quot;;
  3. //answer should be newMessage = &quot;applejuice&quot;
  4. char* concat(char *xMessage, int xSize, char* yMessage, int ySize){
  5. char newMessage[xSize+ySize];
  6. for(....){
  7. //copyxMessage
  8. }
  9. for(....){
  10. //copyYMessage
  11. }
  12. return (char *) newMessage;
  13. }

I know I can use strings, but would preferably keep them as an array of bytes. What could be the best solution for this to avoid the error of returning a local pointer.

SOLUTION: Create a vector or deque of uint8_t depending on the needs of the type of manipulation/algorithms of RAW data. I ended up using a std::vector<uint8_t>. Ex.

std::vector&lt;uint8_t&gt; data(buffer + bytesIn);

Due to in my case receiving data from a socket.

Edit: After reading the replies, the most convenient solution was to create a vector of chars or a string. Since I had a bunch of bytes being sent I decided to go with a std::vector&lt;uint8_t&gt; due to more manipulation I had to do later in the program. Thank you everyone for making me realize that and helping me with this. The reason I was messing with raw char pointers is because the data I was dealing with was RAW data, but converting it into a vector it was easier to manipulate the data, than dealing with char pointers.

答案1

得分: 3

只使用 std::string

  1. std::string concat(char *xMessage, int xSize, char *yMessage, int ySize){
  2. std::string s(xMessage);
  3. s += yMessage;
  4. return s;
  5. }

如果您真的需要指向附加字符串缓冲区的指针,您可以使用一个包装器围绕 concat

英文:

Just use std::string

  1. std::string concat(char *xMessage, int xSize, char* yMessage, int ySize){
  2. std::string s(xMessage);
  3. s += yMessage;
  4. return s;
  5. }

If you really want a pointer to the appended string buffer you can use a wrapper around concat.

答案2

得分: 2

注意:我绝对不同意这样做,但既然您知道可以使用 std::string,但希望使用原始指针来完成,这是一种方法:

  • 分配一个足够大以容纳 xSize + ySize 个字符的缓冲区,再额外多分配一个字符用于终止符 \0。您可以使用 new[] 来完成此操作。
  • 使用 std::memcpy 将输入字符串复制到分配的缓冲区中。
  • 使用后不要忘记使用 delete[] 删除缓冲区。
  1. #include <cstring>
  2. char* concat(const char *xMessage, int xSize, const char* yMessage, int ySize) {
  3. char* newMessage = new char[xSize + ySize + 1];
  4. std::memcpy(newMessage, xMessage, xSize);
  5. std::memcpy(newMessage + xSize, yMessage, ySize);
  6. newMessage[xSize + ySize] = '
    #include <cstring>
  7. char* concat(const char *xMessage, int xSize, const char* yMessage, int ySize) {
  8.     char* newMessage = new char[xSize + ySize + 1];
  9.     std::memcpy(newMessage, xMessage, xSize);
  10.     std::memcpy(newMessage + xSize, yMessage, ySize);
  11.     newMessage[xSize + ySize] = '\0';
  12.     return newMessage;
  13. }
  14. ';
  15. return newMessage;
  16. }

演示

英文:

Note: I absolutely do not agree with doing this, but since you know you can use a std::string but would like to do it with raw pointers, here's one way:

  • Allocate a buffer large enough to hold xSize + ySize characters + 1 extra for the terminating \0. You do this with new[].
  • std::memcpy the input strings into the allocated buffer.
  • Don't forget to delete[] the buffer after use.
  1. #include &lt;cstring&gt;
  2. char* concat(const char *xMessage, int xSize, const char* yMessage, int ySize) {
  3. char* newMessage = new char[xSize + ySize + 1];
  4. std::memcpy(newMessage, xMessage, xSize);
  5. std::memcpy(newMessage + xSize, yMessage, ySize);
  6. newMessage[xSize + ySize] = &#39;\0&#39;;
  7. return newMessage;
  8. }

Demo

答案3

得分: 0

以下是翻译好的部分:

  1. 函数声明
  2. char* concat(char *xMessage, int xSize, char* yMessage, int ySize){
  3. 没有意义。
  4. 首先,如果指针 `xMessage` `yMessage` 指向的数组在函数内部不会被修改,那么相应的参数应该声明为 `const` 限定符。
  5. 或者如果需要将参数 `yMessage` 指向的数组中的字符附加到指针 `xMessage` 指向的数组中,那么无论如何参数 `yMessage` 都应该声明为 `const` 限定符。
  6. 其次,如果参数 `xMessage` `yMessage` 指向字符串,并且您需要构建一个包含连接字符串的新字符串,那么参数 `xSize` `ySize` 没有意义。
  7. 如果确实需要构建一个包含传递的字符串连接的字符串,那么函数可以如下所示:
  8. char * concat( const char *xMessage, const char *yMessage )
  9. {
  10. size_t xLen = strlen( xMessage );
  11. char *result = new char[ xLen + strlen( yMessage ) + 1 ];
  12. memcpy( result, xMessage, xLen );
  13. strcpy( result + xLen, yMessage );
  14. return result;
  15. }
  16. 并且可以像下面这样调用该函数:
  17. char *result = concat( "apple", "juice" );
  18. 函数的调用者负责在不再需要连接的字符串时删除它,例如:
  19. delete [] result;
英文:

The function declaration

  1. char* concat(char *xMessage, int xSize, char* yMessage, int ySize){

does not make sense.

Firstly, if the arrays pointed to by the pointers xMessage and yMessage are not changed within the function then the corresponding parameters should be declared with qualifier const.

Or if characters from the array pointed to by the parameter yMessage are appended to the array pointed to by the pointer xMessage then in any case the parameter yMessage should be declared with qualifier const.

Secondly, if the parameters xMessage and yMessage point to strings and you need to build a new string that will contain concatenated strings then parameters xSize and ySize are senseless.

If you indeed need to build a string that will contain concatenated passed strings then the function can look the following way

  1. char * concat( const char *xMessage, const char *yMessage )
  2. {
  3. size_t xLen = strlen( xMessage );
  4. char *result = new char[ xLen + strlen( yMessage ) + 1 ];
  5. memcpy( result, xMessage, xLen );
  6. strcpy( result + xLen, yMessage );
  7. return result;
  8. }

And the function can be called for example like

  1. char *result = concat( &quot;apple&quot;, &quot;juice&quot; );

The caller of the function is responsible for deleting the concatenated string when it will not be required any more like

  1. delete [] result;

huangapple
  • 本文由 发表于 2023年6月9日 05:38:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76435853.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定