0

Per the msdn example here, I am trying to convert char* (specifically from main() argv) to a wchar_t*

the code works alright by itself but when i try to wrap it in a function for reuse, i find that the wchar_t* dst is being garbled upon exit of the wrapping fuction. i get an error for uninitialized memory for passing in a pointer i didn't initialize outside of the function, and an access violation after it exits.

But that's the thing. i WANT to pass in a pointer to a wchar_t that isn't set, set it in function, and use the set value after the fact. what am I doing wrong here?

here's my example code:

int main(int argc, char* argv[])
{

    wchar_t* dst;
    for (int i = 0; i < argc; i++) 
    {           
        char_str_to_wchar_t_str(dst, argv[i]);
        
        // do some stuff with dst, like print it.
        // but by the time i get here, dst is corrupted. why? 
        // and what do i do to fix it?
        
    }
}

static size_t char_str_to_wchar_t_str(wchar_t* dst, char* src)
{
    size_t newsize = strlen(src) + 1;
    dst = new wchar_t[newsize]; // Convert char* string to a wchar_t* string.
    size_t convertedChars = 0;
    mbstowcs_s(&convertedChars, dst, newsize, src, _TRUNCATE);
    // at this point, dst is correctly converted and i can read it fine in the debugger. 
    return convertedChars;
}

EDIT: don't see how the question this was associated with helps. also as an aside, i'm doing this because i'm up-converting some c code to c++. it looks like converting it to a std::wstring and returning that accomplishes the same basic thing, and cleaner IMO, but the original question remains: if i'm passing a pointer as an argument into the function, then i should be able to use that pointer after the function exits, what's wrong with that?

apierion
  • 1
  • 1
  • 2
    The problem has nothing to do with strings, and everything to do with passing an uninitialized pointer to the `char_str_to_wchar_t_str` function by value. – PaulMcKenzie Sep 09 '20 at 15:57
  • Change `static size_t char_str_to_wchar_t_str(wchar_t* dst, char* src)` to `static size_t char_str_to_wchar_t_str(wchar_t* & dst, char* src)` related: [https://stackoverflow.com/a/10257398/487892](https://stackoverflow.com/a/10257398/487892) – drescherjm Sep 09 '20 at 16:00
  • *i WANT to pass in a pointer that isn't set, set it in function, and use the set value after the fact* -- If you replace the word "pointer" with the word "int", you should see the error. Pass-by-value works on temporaries within the function -- you don't see the results on return. – PaulMcKenzie Sep 09 '20 at 16:03
  • 2
    There's a big difference between changing the pointer and changing the value that the pointer points to. You are mixing up two completely different things. Just because you passed a completely uninitialized pointer ***by value*** to a function (which every self-respecting C++ compiler will warn you about), and you finally initialized that pointer in the function, doesn't mean that the uninitialized pointer in the original function is somehow initialized. C++ doesn't work this way. This is what references are for. – Sam Varshavchik Sep 09 '20 at 16:08
  • @PaulMcKenzie i know it has nothing to do with strings. maybe that part wasn't clear. even if you initialize the pointer before passing it in, the pointer gets reinitialized again inside the function and the data gets corrupted/lost. – apierion Sep 09 '20 at 16:10
  • Well, then, don't initialize again inside the function. Easy problem to solve. Or, initialize, in the function, a passed by reference parameter. – Sam Varshavchik Sep 09 '20 at 16:10
  • @SamVarshavchik let me ask another way: say what i'm trying to do is wrap mbstowcs_s in another function that takes less input, just the src and the dst. so i want to write something like ```void char_to_wchar_t(wchar_t* dst char* src);``` - edit: as i was writing this it came together - basically - the initialization inside the function is the problem. i'm trying to do something i shouldn't be doing... – apierion Sep 09 '20 at 16:15
  • It will not work with this: `void char_to_wchar_t(wchar_t* dst char* src);` signature. – drescherjm Sep 09 '20 at 16:18
  • so i got around it by returning a wstring - we're in c++, makes things easier. but ultimately i'm trying to create a wargv[] - a wide version of char* argv from main. this works: std::wstring warg = char_to_wstring(argv[i]); but this next line produces garbage: wargv[i] = (wchar_t*)warg.c_str(); // wargv[i] is populated, but junk data – apierion Sep 09 '20 at 16:42
  • 1
    @apierion *then i should be able to use that pointer after the function exits* -- Take a simple, non string example: `void foo(int *x) { x = new int[10];} int main() { int *x = nullptr; foo(x); }` you will see that `x` is still `nullptr` after you called `foo` -- it hasn't changed. The duplicate tells you why it hasn't changed, even though you issued a `new[]` call inside of `foo`. The code has two mistakes -- you passed the pointer by value, and **anything** passed by value is a temporary, including pointers. The second mistake is that the first mistake now caused a memory leak.. – PaulMcKenzie Sep 09 '20 at 18:49

0 Answers0