简体   繁体   English

接收指向C样式字符串的指针作为参数并能够分配或修改内存的正确方法是什么?

[英]What is the correct way to receive a pointer to a C-style string as an argument and be able to allocate memory or modify it?

I have the following method signature: 我有以下方法签名:

int get_name(char* &name_out);

What I'd like to be able to do is allocate memory that is the size of name for name_out and then copy the contents of name into name_out . 我想要做的就是分配与name_outname大小的内存,然后将name的内容复制到name_out

Or would it be better to change it to: 还是将其更改为:

int get_name(char &name_out[], int size);

and allow the user of the function to first allocate and track the memory, and return an error if the size of the given array isn't large enough to contain the string? 并允许函数的用户首先分配和跟踪内存,如果给定数组的大小不足以容纳字符串,则返回错误?

The only thing that I don't like about that is that it would require the user of the get_name() function to have knowledge about the length of the name string. 我唯一不喜欢的事情是,它将要求get_name()函数的用户了解名称字符串的长度。

I feel it would be redundant to have two functions int get_name_length(); 我觉得拥有两个函数int get_name_length();是多余的int get_name_length(); and get_name(char* name_out); get_name(char* name_out);

Since this is a piece of a programming assignment, there are stipulations: 由于这是编程任务的一部分,因此有一些规定:

  1. The string class is not allowed. 不允许使用字符串类。
  2. C-style strings must be used. 必须使用C样式的字符串。
  3. Vectors cannot be used. 不能使用向量。
  4. The function must return an int to indicate an error. 该函数必须返回一个int以指示错误。
  5. Exception handling is not allowed. 不允许进行异常处理。

Thank you. 谢谢。

If I understand correctly what you are trying to do is to implement a variant for 'strcpy'. 如果我理解正确,那么您要尝试实现的是'strcpy'的变体。

The major difference is that you pass the allocation responsibility to your copy function, while 'strcpy' leaves that to the user. 主要区别在于,您将分配职责传递给了复制功能,而“ strcpy”则留给了用户。 If this is a production code then I recommend following the 'strcpy' approach which is what the industry is accustomed to. 如果这是生产代码,那么我建议您遵循行业习惯的“ strcpy”方法。

If it's just for play then wrap strcpy with a function that does the allocation and stick to strcpy's interface. 如果只是为了娱乐,则将strcpy与执行分配的函数包装在一起并坚持使用strcpy的界面。

The standard C way to do this, is to pass two pointers to the function: 执行此操作的标准C方法是将两个指针传递给该函数:

int getName(char** name_out, size_t* size_out);

This has several advantages: 这有几个优点:

  • The caller is free to preallocate memory/reuse an allocation. 调用者可以自由地预分配内存/重新使用分配。

  • The callee is free to adjust the allocation via realloc() (assuming that the C-style string is allocated with malloc() ), or via a pair of delete[] / new[] . 被调用方可以通过realloc()随意调整分配(假设C风格的字符串是通过malloc()分配的),或者通过一对delete[] / new[]

  • The address taking is explicit. 地址获取是明确的。 Ie at the calling site you would write: 即在呼叫站点,您将编写:

     char* name = null_ptr; size_t size = 0; if(getName(&name, &size)) handleError(); 

    The explicit & operator makes it very clear that the function getName() can change both variables. 显式&运算符非常清楚,函数getName()可以更改两个变量。 If you go with references, you cannot distinguish between call by reference and call by value without looking at the function declaration. 如果使用引用,则不查看函数声明就无法区分按引用调用和按值调用。

Also note, the type to use for allocation sizes is size_t : it is the type that is guaranteed to be able to hold the size of the entire usable address space. 还要注意,用于分配大小的类型是size_t :它是保证能够容纳整个可用地址空间大小的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM