473,503 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using C types with garbage collection

Is it possible to use "normal" C types with garbage collection? Here's
the function I'd like to write, but not have to worry about deallocation:
char* ConvertStringToChar(String* pString)
{
const wchar_t __pin *pStrChars;

char* pOutString = (char*)malloc(sizeof(char) * 256);

pStrChars = PtrToStringChars(pInString);
wcstombs(pOutString, pStrChars, (pInString->Length + 1) * 2);

return pOutString;
}

And then use it such as:

MessageBoxA(NULL, ConvertStringToChar(new String("Hello")), "Test", MB_OK);
Thanks,

Gary
Nov 17 '05 #1
1 891
"Gary Nastrasio" <ga***********@hotmail.com> wrote in message
news:eV**************@TK2MSFTNGP14.phx.gbl...
Is it possible to use "normal" C types with garbage collection? Here's
the function I'd like to write, but not have to worry about deallocation:
char* ConvertStringToChar(String* pString)
{
const wchar_t __pin *pStrChars;

char* pOutString = (char*)malloc(sizeof(char) * 256);

pStrChars = PtrToStringChars(pInString);
wcstombs(pOutString, pStrChars, (pInString->Length + 1) * 2);

return pOutString;
}

And then use it such as:

MessageBoxA(NULL, ConvertStringToChar(new String("Hello")), "Test",
MB_OK);


I'm not sure I understand what you are asking. But, if you are asking if you
can allocate memory with malloc() and have the garbage collector deallocate
it, then the answer is no.

BTW, while we are on the subject of C-isms, do you know about the _alloca()
function. It makes allocations from the stack. These allocations evaporate
into the ether upon leaving the function in which the call was made without
any effort on your part. So, you couldn't use it in your conversion
function, but you could use it immediately before the call to MessageBoxA().

Regards,
Will
Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
448
by: codymanix | last post by:
Last night I had several thought about RAII and want to discuss a bit. Why doesn't CSharp support destructors in structs? Wouldn't that make RAII possible like in C++? When the struct goes out of...
0
1855
by: Andrew | last post by:
When will .NET have a low-pause-time garbage collector A low-pause-time garbage collector would greatly improve .NET's ability to serve as a platform for soft real-time systems. It doesn't have...
11
2712
by: Rick | last post by:
Hi, My question is.. if Lisp, a 40 year old language supports garbage collection, why didn't the authors of C++ choose garbage collection for this language? Are there fundamental reasons behind...
4
2026
by: Pedro Miguel Carvalho | last post by:
Greetings. I'm creating a project that as a intricate relation between object kind of like a set where each object in the set can be connect to a subset of object of the set and objects not in...
5
3556
by: Bob lazarchik | last post by:
Hello: We are considering developing a time critical system in C#. Our tool used in Semiconductor production and we need to be able to take meaurements at precise 10.0 ms intervals( 1000...
14
2680
by: Piotrek | last post by:
Hi all. I have a web app, in which I use frames. My main frameset consists of three inner frames. When some button is pressed in frame A, then content of frame B is reloaded. I am using such...
350
11457
by: Lloyd Bonafide | last post by:
I followed a link to James Kanze's web site in another thread and was surprised to read this comment by a link to a GC: "I can't imagine writing C++ without it" How many of you c.l.c++'ers use...
53
2575
by: None | last post by:
Hi, I am a game developer, sometimes "indy" and sometimes for a small-sized company. I can't speak for all game developers, but everywhere I've ever seen people working on games, execution...
158
7690
by: pushpakulkar | last post by:
Hi all, Is garbage collection possible in C++. It doesn't come as part of language support. Is there any specific reason for the same due to the way the language is designed. Or it is...
0
7198
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
7072
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
7271
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
7319
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5570
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4998
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3149
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.