function parameter issue?

Hi all...

Just starting out with c++.. I've been programming for a long time but not very long with c/c++...

See below; a function that converts an ASC string to a Unicode friendly format for use with a control that requires Unicode strings.

It works just fine when it's not written as a function and is passed the initial ASC string directly in code.


*******************

this is a test

*******************


However, when the ASC string is passed via the function parameter I get this:

*******************

this<ꎳ@폔@<

*******************


I assume this has something to do with pointers that I'm missing... I realize there may be a better way to do this but I need to understand why this does what it does... :/

Thanks for any ideas...


1
2
3
4
5
6
  	void print_txt(char asc_in[])
	{
	wchar_t uni_out[ sizeof(asc_in) ];
	mbstowcs(uni_out, asc_in, sizeof(asc_in));
	Form1->Memo1->Text = uni_out;
	}
Last edited on
Do not use sizeof(asc_in). The result is the length of the pointer. Not the string. Use strlen(asc_in) instead:

http://www.cplusplus.com/reference/cstring/strlen/?kw=strlen
Thanks.

I tried that but I get a const required for asc_in... So I tried using char const asc_in but it doesn't help...
In that case you need to dynamically allocate the required buffer:
1
2
3
4
5
6
7
  	void print_txt(char asc_in[])
	{
	wchar_t *uni_out = new wchar_t[ strlen(asc_in) + 1 ]; // Note: + 1 for the terminating 0
	mbstowcs(uni_out, asc_in, strlen(asc_in) + 1);
	Form1->Memo1->Text = uni_out;
	delete[] uni_out; // Don't forget this
	}
Last edited on
Yes this works... Thank you.

Unfortunately, while I understand the code, I am lost as to *why* it must be written like this. Is there anything in particular that I might study in order to get a better handle on the whys and hows of this area of cpp?

Also if I might ask.. Would it be possible to write this function so that the uni_out array is returned in the function?

Thanks again for the fast reply...
Last edited on
The reason why is the difference between compile time and runtime.

If the memory size is known at compile time the stack can be used to store the memory.

If the memory size is known at runtime the heap (with new/delete) is used to store the memory.

Also the size of the heap is way larger than the stack. Hence you will store large object on the heap in order to avoid corruption of the stack. There are more reason you might learn later (like inheritance of classes).

Would it be possible to write this function so that the uni_out array is returned in the function?
Yes, this is another reason to use the heap. Objects on the stack exists as long as the surrounding function processes. After the function ends the stack memory for the function is freed.

1
2
3
4
5
6
7
  	wchar_t *print_txt(char asc_in[])
	{
	wchar_t *uni_out = new wchar_t[ strlen(asc_in) + 1 ]; // Note: + 1 for the terminating 0
	mbstowcs(uni_out, asc_in, strlen(asc_in) + 1);
	Form1->Memo1->Text = uni_out;
	return uni_out; // Don't forget to delete[] uni_out; later
	}
Thanks very much for the help. I'll be working on wrapping my brain around this stuff.. I get much of it conceptually but at times faced with a problem I don't see it.

Hopefully with time I'll get better at this...
Topic archived. No new replies allowed.