Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6385487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:54:21+00:00 2026-05-25T02:54:21+00:00

In C++/CLI, What’s the most efficient way to convert an array of strings to

  • 0

In C++/CLI, What’s the most efficient way to convert an array of strings to native char**?

I am doing this:

array<String^>^ tokenArray = gcnew array<String^> {"TokenONE", "TokenTWO"};
int numTokens = tokenArray->Length;
char** ptr = new char* [numTokens];
for(int i = 0; i < numTokens; i++)
    {
        // See: http://stackoverflow.com/questions/6596242/
        array<Byte>^ encodedBytes = Text::Encoding::UTF8->GetBytes(tokenArray[i]);
        pin_ptr<Byte> pinnedBytes = &encodedBytes[0];
        ptr[i] = reinterpret_cast<char*>(pinnedBytes);
    }
int myResult = someNativeFunction(ptr, numTokens);
delete ptr;
// ...

What, if anything should be improved? Is this ok from a memory management point of view? I can change the parameters of someNativeFunction if need be.

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:54:22+00:00Added an answer on May 25, 2026 at 2:54 am

    Apart from the problem with pinned pointers going out of scope before being passed to someNativeFunction(), the code can be simplified for better clarity especially if you’re using MSVC2008 or newer. See this page for information on how to convert a single string (extending to an array should be trivial).

    Edited:

    If you need ANSI strings const char* then making a copy is inevitable since .NET Strings are Unicode (UTF-16). On MSVC2008 and newer, your code may look as follows:

    #include <msclr/marshal.h>
    using namespace msclr::interop;
    
    marshal_context context;
    array<String^>^ tokenArray = gcnew array<String^> {"TokenONE", "TokenTWO"};
    char** tokensAsAnsi = new char* [tokenArray->Length];
    
    for(int i = 0; i < tokenArray->Length; i++)
    {
        tokensAsAnsi[i] = context.marshal_as<const char*>(tokenArray[i]);
    }
    int myResult = someNativeFunction(ptr, tokensAsAnsi);
    
    // The marshalled results are freed when context goes out of scope
    delete[] tokensAsAnsi;    // Please note you must use delete[] here!
    

    This does similar job to your code sample but without the need of pointer pinning and reinterpret_cast-ing.

    If you are willing to deal with wide string const wchar_t* in someNativeFunction(), you can use the (pinned) internal data directly, However, you’ll have to ensure the pointers remain pinned until someNativeFunction() returns which, as pointed out in the comments, may negatively influence the GC performance.

    If you’re about to marshall many strings and performance is of utmost concern, you could
    split the marshalling across several threads before passing everything to someNativeFunction(). Before doing that, I’d reccommend profiling your application to see if the conversion really is a bottleneck or whether it’s better to focus efforts elsewhere.

    Edited #2:

    To get the native string in UTF-8 encoding, you can do with a modified version of your code:

    array<String^>^ tokenArray = gcnew array<String^> {"TokenONE", "TokenTWO"};
    char** tokensAsUtf8 = new char* [tokenArray->Length];
    
    for(int i = 0; i < tokenArray->Length; i++)
    {
        array<Byte>^ encodedBytes = Text::Encoding::UTF8->GetBytes(tokenArray[i]);
    
        // Probably just using [0] is fine here
        pin_ptr<Byte> pinnedBytes = &encodedBytes[encodedBytes->GetLowerBound(0)];
    
        tokensAsUtf8[i] = new char[encodedBytes->Length + 1]; 
        memcpy(
            tokensAsUtf8[i], 
            reinterpret_cast<char*>(pinnedBytes),
            encodedBytes->Length
            );
    
        // NULL-terminate the native string
        tokensAsUtf8[i][encodedBytes->Length] = '\0'; 
    
    }
    int myResult = someNativeFunction(ptr, tokensAsAnsi);
    
    for(int i = 0; i < tokenArray->Length; i++) delete[] tokensAsUtf8[i];
    delete[] tokensAsUtf8;    
    

    If you’re concerned about speed, you could pre-allocate a large buffer for the native strings (if you know there will only be a limited amount) or use pool storage.

    Edited #3:(OG Dude)
    Just fixed some minor typos.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using C++ CLI. I need to convert usual char * to cli array.
I'm reading C++/CLI. I see this stuff: Object^ CreateInstanceFromTypename(String^ type, ...array<Object^>^ args) { if
In my C++\CLI i have this piece of code: array<Byte>^ out_buf = gcnew array<Byte>(stream_size);
In managed C++/CLI, I could do this either as (1): array<System::Byte>^ css_keycode = {0x51,
In C++/CLI , you can use native types in a managed class by it
When running PHP in CLI mode, most of the time (not always), the script
I have a C++/CLI wrapper around native .lib and .h files. I use the
I have a CLI script which when you first start it: function __construct(){$this->connectToDatabase();} protected
Is there a C++/CLI RAII smart pointer class for containment of a native pointer
If I run this CLI command: pdftotext -l 10 file.pdf - | findstr /i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.