Help with Managed C++ conversion (char[] to String __gc*)

  • Thread starter Thread starter rickey tom
  • Start date Start date
R

rickey tom

I'm from the C++ world and the following code does not look right.
Please confirm my suspicions!!!

I have the following method

String __nogc* foo()
{
char someBuffer[1000];
strcpy(someBuffer,"test");

// The compiler accepts this and the program seems to behave fine
return someBuffer;

// My gut feels says that the code really should be
return new String(someBuffer);
}

I'm using Microsoft Visual C# .NET IDE.

Any help here would be greatly appreciated.

Thanks
Rick
 
It's OK. this return statement implicitly constructed a temp string using
the char[]. Your location char[] is copied in the process, so you don't
need to worry about whether they are valid or not after the return.
Qiu

Rickey Tom said:
Nobody has an answer for this??

rickey tom said:
I'm from the C++ world and the following code does not look right.
Please confirm my suspicions!!!

I have the following method

String __nogc* foo()
{
char someBuffer[1000];
strcpy(someBuffer,"test");

// The compiler accepts this and the program seems to behave fine
return someBuffer;

// My gut feels says that the code really should be
return new String(someBuffer);
}

I'm using Microsoft Visual C# .NET IDE.

Any help here would be greatly appreciated.

Thanks
Rick
 
Back
Top