Why not change func1 such that it takes a std::string instead?
If you cannot do that, one solution is to create an array of char of sufficient size, copy over the contents of the std::string (appending a null character), then pass a pointer to the first character of this new array to the function.
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
I would be wary doing that. Notice the function takes a pointer to non-const. what if the function uses strcat and alters the length of the string that then passes the vectors capacity. Because you are accessing internals of the vector without going through vectors own functions for doing such, it could be problematical imo as the vector will lose internal consistency. This is one occaision where I may choose to manage the memory allocations personally or at the very least make sure I have reserved a fair bit of spare space in the vector. it all depends on exactly what the function will do, to which i could see no details.
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
If the function needs its argument to be of a certain size, then that's going to be a problem no matter *how* the memory is allocated. So you may as well use a vector sized appropriately.
I personally prefer working with "end" iterators then using size type constructs. It is usually safer. forgetting the +1 after length can be easy. Also, its presence is (imo) confusing.
I would be wary doing that. Notice the function takes a pointer to non-const. what if the function uses strcat and alters the length of the string that then passes the vectors capacity.
How is that any different than allocating too little space using manual memory management?
Because you are accessing internals of the vector without going through vectors own functions for doing such,
That is exactly the goal, and that is to use std::vector as just a buffer. It is guaranteed that a vector stores its buffer in contiguous memory, just like any array.
This is one occaision where I may choose to manage the memory allocations personally or at the very least make sure I have reserved a fair bit of spare space in the vector. it all depends on exactly what the function will do, to which i could see no details.
You are not improving anything by managing your own memory in this case. Whatever memory you reserved doing manual memory management will encounter the same issues if you don't size your vector appropriately.
Whatever you did "manually", you can do the same thing with vector. Give us a case where you have managed memory manually (using new[] and delete[]) where a vector couldn't be used.
Who is responsible for cleaning up the memory? Why introduce dynamic allocation? The solutions that were given using vector are far superior and safer.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.