By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,304 Members | 2,021 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,304 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Nobody has an answer for this??

"rickey tom" <ri********@hotmail.com> wrote in message
news:O%****************@TK2MSFTNGP12.phx.gbl...
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

Nov 15 '05 #2

P: n/a
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" <ri********@hotmail.com> wrote in message
news:Of**************@TK2MSFTNGP11.phx.gbl...
Nobody has an answer for this??

"rickey tom" <ri********@hotmail.com> wrote in message
news:O%****************@TK2MSFTNGP12.phx.gbl...
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


Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.