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

  • Home
  • SEARCH
  • 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 8335425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:40:27+00:00 2026-06-09T03:40:27+00:00

we are currently digging through some really old C++/CLI-Code (Old Syntax .NET Beta) and

  • 0

we are currently digging through some really old C++/CLI-Code (Old Syntax .NET Beta) and were a bit surprised to see something like this:

System::String ^source("Test-String");
printf("%s", source);

The program correctly outputs

Test-String

We are wondering, why is it possible to pass the managed string source to printf – and more importantly: Why does it work? I don’t expect it to be some convenience-feature by the compiler because the following doesn’t work:

System::String ^source("Test-String");
char pDest[256];
strcpy(pDest, source);

This produces a (somehow expected) compiling error saying that System::String^ can’t be converted to const char*. So my only real explanation is that passing a managed reference to a va_list surpasses all compiler-checks and tricks the native code into using a pointer into the managed heap. Since System::String is represented similar to a char-Array in memory, printf may work. Or the compiler converts to a pin_ptr and passes that to printf.

I don’t expect it to automatically marshal the String^ to char*, because that would result in a bad memory leak without any reference to the actual memory address.

We know that this isn’t a good solution and the various marshalling methods introduced by the later Visual Studio-Versions provide a way better approach but it would be very interesting to understand what is actually happening here.

Thanks!

  • 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-06-09T03:40:29+00:00Added an answer on June 9, 2026 at 3:40 am

    I believe it is because the compiler is turning it into this IL:

    call vararg int32 modopt([mscorlib]System.Runtime.CompilerServices.CallConvCdecl) printf(int8 modopt([mscorlib]System.Runtime.CompilerServices.IsSignUnspecifiedByte) modopt([mscorlib]System.Runtime.CompilerServices.IsConst)*, ..., string)
    

    Which ends up as a pinvoke call to printf, so the runtime is being a little sneaky by marshalling it for you. You are still in a managed runtime, and the runtime will provide marhsalling as a service when it’s needed.


    Some notes:

    It seems that clr!GenericPInvokeCalliHelper is doing this lifting on the x86 .NET 4 Workstation CLR.

    the following doesn’t work

    That’s because that is straight C++. It has no chance to go through marshalling because it isn’t needed.

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

Sidebar

Related Questions

I have been trying to optimize some code which handles raw pixel data. Currently
I have digging through the C# code generated by SWIG for Quantlib and came
While debugging some code in gdb, I want to see which line will be
Currently I have this code: <?php echo '<meta name=robots content=noindex>'; $arr = json_decode(file_get_contents(http://media1.clubpenguin.com/play/en/web_service/game_configs/ paper_items.json),true);
So I was tasked at replacing some dummy code that our project requires for
So I am digging through the WebOS enyo framework and am getting very frustrated.
I'm digging through Google's results for free (open source) Java diff libaries, and there
I'm currently creating a new neat CLI library for PHP, and i'd like to
I am digging through OpenERP web to find how I can remove the exception
Currently, I am writing a MiddleWare application that synchronizes information between and accounting application

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.