0

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.

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.

I feel it would be redundant to have two functions int get_name_length(); and 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.
  3. Vectors cannot be used.
  4. The function must return an int to indicate an error.
  5. Exception handling is not allowed.

Thank you.

Nathan Lutterman
  • 1,855
  • 4
  • 23
  • 38
  • 1
    Why not `int copy_string(const char* src, char*& dest);`? Then you document that the function allocates memory that must be `delete`d. Or you could use a smart pointer instead. It isn't on your list of things you can't use. – juanchopanza Jan 29 '15 at 06:25
  • I thought about just documenting that the function allocates memory that must be deleted. I'm not familiar with smart pointers, yet. I'm in an introductory class and we're basically learning how to break software with pointers and pointer arithmetic, so I think I'll add the stipulation that those are prohibited as well. – Nathan Lutterman Jan 29 '15 at 06:28

2 Answers2

1

If I understand correctly what you are trying to do is to implement a variant for 'strcpy'.

The major difference is that you pass the allocation responsibility to your copy function, while 'strcpy' leaves that to the user. If this is a production code then I recommend following the 'strcpy' approach which is what the industry is accustomed to.

If it's just for play then wrap strcpy with a function that does the allocation and stick to strcpy's interface.

Ezra
  • 1,401
  • 5
  • 15
  • 33
0

The standard C way to do this, is to pass two pointers to the function:

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[].

  • The address taking is explicit. I.e. 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. 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.

cmaster - reinstate monica
  • 38,891
  • 9
  • 62
  • 106
  • This being C++, it might be more idiomatic to pass a reference to pointer rather than a pointer to pointer for the first parameter, and a reference for the second. – juanchopanza Jan 29 '15 at 06:32
  • cmaster made the comment that having the & operator in the parameters does not make the fact that it's pass by reference explicit. This may be a matter of opinion - but is it more idiomatic to pass by reference rather than by pointer in C++? – Nathan Lutterman Jan 29 '15 at 06:43
  • 1
    @Zaemz: In general, that's a matter of opinion. Coding standards often have preferences. – Bill Lynch Jan 29 '15 at 06:46
  • @Zaemz Yes it is more idiomatic to pass by reference in C++. The don't pass by reference thing `&` is for lazy beginners who call functions without knowing what they do. In my opinion it is best to confuse these people early on. – juanchopanza Jan 29 '15 at 06:46
  • 1
    @Zaemz I believe most C++ programmers would go for the reference, despite the fact that it makes the code less clear to read. However, the explicit `&` is idiomatic for C, and the constraints in your question are also C-style constraints, so you wouldn't break with your prescribed style. Personally, I either use a `const` reference which has the same semantics as pass by value, true pass by value, or explicit `&` for clarity: When I read code I want to be able to see what it can change without hunting down all the involved function declarations. – cmaster - reinstate monica Jan 29 '15 at 06:51