473,411 Members | 2,078 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

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

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
2 5328
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Alper AKCAYOZ | last post by:
Hello Esteemed Developers and Experts, I have been using Microsoft Visual C++ .NET for 1 year. During this time, I have searhed some topics over internets. Most of the topics about .NET is...
27
by: Trep | last post by:
Hi there! I've been having a lot of difficult trying to figure out a way to convert a terminated char array to a system::string for use in Visual C++ .NET 2003. This is necessary because I...
0
by: Ben Harper | last post by:
I am stuck using VC7 right now, so I can't test this on 7.1, but I believe it is a bug in the compiler, although I hope it isn't. Please could someone confirm this for me, prove me wrong, or suggest...
2
by: mccoyn | last post by:
I am trying to use a C style string in a managed class. When I try to call a function like strcmp I get an error. For example, this code __gc MyClass char str _nogc void foo() strcpy(str,...
7
by: Bob Rock | last post by:
Hello, converting from the managed to the unmanaged world (and viceversa strings) and byte arrays is something I do often and I'd like to identify the most correct and efficient way to do it....
3
by: Steve | last post by:
How can I copy 5 bytes from the middle of one array to another in Managed C++? The following code segment causes the compilation errors below: unsigned char cResult __gc = new unsigned char __gc;...
6
by: dlgproc | last post by:
Hi I’m trying to write a C++ class library in Visual Studio 7.1 .NET using managed extensions that calls code in an unmanaged static library. Functions in the static library have parameters...
6
by: Tim | last post by:
I'm trying to co-erce a __gc array of Byte to a __nogc pointer to char to pass to a native function call in a bit of managed c++ code like this: Byte field __gc = dynamic_cast<Byte...
3
by: James | last post by:
I'm interested in learning C++. I'm not sure exactly what this means. I'm currently a VB.NET/C#.NET programmer (both Windows Forms and ASP.NET). I am totally clueless when it comes to C++. I've...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.