Avoid returning dynamic arrays from functions?

Hi!
I have function:

1
2
3
4
5
6
7
8
const wchar_t * CharToWChar( const char * text ) {
    unsigned size = 0;
    while( text[ size++ ] );
    wchar_t * wchar_arr = new wchar_t[ size ];
    for( unsigned i = 0; i < size; ++i )
         wchar_arr[i] = text[i];
    return wchar_arr;
}


It converts char* to wchar_t*. I've got problem, 'cuz before program exits I need to use delete[], function returns dynamic array. Is it possible to do, so that dynamic array is deleted when function runs, and instead of pointer to that array returned is pointer to wchar_t that doens't need to be deleted? I'd like to avoid using any class objects or mbstowcs function to convert char* to wchar_t*.
Last edited on
Hello TheHardew. So that I'm clear, I think you are asking if the wchar_t array can point to the char array instead of the allocated array? Unless I'm wrong, you can't do that (char is 8bit and wchar is 16bit). Maybe I'm missunderstanding the question, and you are asking of the char array can be deleted once it is done being used. In that case, you can add a delete command.
delete text; // between line 6 and 7

Sorry if neither of these 2 comments help you. Thanks, Spike :D
Sorry that was just stupid mistake. I meant that when you declare wchar_t* in main that points to "Foo" you don't have to delete that when you exit program. I'd like my function to return pointer to wchar_t string the same way, you just use function and then you don't worry about anything.
when you declare wchar_t* in main that points to "Foo" you don't have to delete that when you exit program.

Wrong. You should deallocate all dynamically allocated memory appropriately.

How about using std::string and std::wstring (despite your dislike of classes)? Those do all the necessary new/delete "under the hood".

For their conversion, see: http://stackoverflow.com/questions/246806/i-want-to-convert-stdstring-into-a-const-wchar-t
Last edited on
Classes are not so efficient , right? And what I meant about that wchar_t* is the following:
1
2
3
4
5
int main(){
    const wchar_t* text = L"Foo";
    std::wcout <<  text; // I don't have to delete this - it wasn't dynamically allocated
    return 0;
}

Isn't there any C-style solution?
Classes are efficient.

How do you allocate for the char array that you call the function with?
1. string uses 28 bytes of memory(at least sizeof(std::string)), wchar_t* only 4 so I thought wchar_t* is more efficient.
2. That's how I call my function:
const wchar_t* text = CharToWChar("Foo");
Last edited on
1. The size of std::wstring depends on implementation (i.e. the vendor of compiler) and does not necessarily include the size of the actual stored string (although some implementations supposedly store short strings within the object).

Efficiency, however, is much more than just the size. The code in standard library has solid design, documentation, and has been tested. You can trust it to do the Right Thing. Use of standard library makes your code simpler and much easier to maintain. You should focus on the problem that your program should solve rather than low-level language details that the std already takes care of.

2. So the input is a constant string literal? Not even a static array. However, you already have that L"Foo" option, so the need for a (constexpr) function is obscure.

The design of your function as it is now states that it allocates memory dynamically and transfers the ownership of that memory to the caller. That makes the caller responsible for deallocating the memory at exit. However, there are many potential paths of exit, so the C++ way of today is to use a smart pointer instead of plain pointer. Smart pointer has a destructor function that is called no matter how you exit the scope and that destructor will deallocate the memory. Likewise, the std::wstring has destructor for cleaning up at end the memory that the object "owns".

Alas, the std::unique_ptr is a class object!
Thanks for time you spent to help me. I'm a little bit sad to read it's not that simple, I've got to use classes. Maybe I'll just edit this function so it's method of class, than destuctor'd automatically delete dyanamic array. And I know I can use L"Foo", but I made this function 'cuz if I read contents of file and display using WinApi it's something like Chinese. Then I have to somehow convert it, I wanted to automate this as much as possible.
Maybe I'll just edit this function so it's method of class, than destuctor'd automatically delete dyanamic array.

Please, use the std::string and std::wstring instead. Learning to use them is likely to be more useful than learning to write a class that imitates a tiny portion of them.

I know I can use L"Foo", but I made this function 'cuz if I read contents of file

If so, then you cannot use L"Foo", because you don't have that "Foo" either. You won't get raw string literal constants from a file.

You have to reserve space for the data that you will read from a file. A read is essentially a copy operation. You either reserve an array statically and make sure that you don't get a buffer overflow or you use something that can automatically and dynamically reserve necessary space. The std::string and std::wstring can.
Ok thanks, I'll use string and wide string, or just return smart pointers
Topic archived. No new replies allowed.