473,386 Members | 1,883 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,386 software developers and data experts.

Managed Pointers On Heap (!)

Hi folks,
I'm new to Managed C++. I would like to know how can I implement a function
like this:

CWhateverClass **GetPointer()
{
//how can I allocate memory on heap (for my local variable of type
CWhateverClass),
//so that it won't go away when the
//local variable is out of scope.
}

Any help would be highly appreciated,

Cheers.
Mehdi
Jul 21 '05 #1
2 1684
Mehdi Mousavi wrote:
Hi folks,
I'm new to Managed C++. I would like to know how can I implement a
function like this:

CWhateverClass **GetPointer()
{
//how can I allocate memory on heap (for my local variable of type
CWhateverClass),
//so that it won't go away when the
//local variable is out of scope.
}


You don't allocate memory, instead, you create objects. If you think in
terms of objects and object references then it makes life far easier. The
nuts and bolts of memory allocation are the responsibility of the GC, not
you!

With that in mind you don't need a pointer to a pointer, instead try this:

CWhateverClass __gc * GetPointer()
{
CWhateverClass __gc * obj = __gc new CWhateverClass;
// stuff
return obj;
}

// other code
CWhateverClass __gc * ptr = GetPointer();
// [1]

The point is that the __gc object is created on the managed heap, and a
*reference* to that object is returned. At the end of the method there are
two references, and once the stack has been cleared the local variable obj
goes out of scope and hence only the reference ptr will exist. However,
since at least one reference to the object exists it means that *if* a
garbage collection occurs at point [1] then the object will not be removed.

Note that my terminology is a little hazy here because at point [1] both the
reference ptr and obj will exist, but the GC will not be able to obtain the
reference obj (it will exist somewhere in memory, but the GC will not be
able to access it and so it is 'unreachable') and hence the GC ignores it.

Richard
--
my email ev******@zicf.bet is encrypted with ROT13 (www.rot13.org)
sign up for my free .NET newsletter at
http://www.wd-mag.com/newsletters/
Jul 21 '05 #2
Well, it sounds amazing. Would you please answer one more question? I've
noticed the "__gc" keyword before the "new" one:

CWhateverClass __gc * obj = __gc new CWhateverClass;

is it neccessary to instruct the compiler (explicitly) that the pointer has
to be garbage collected with mentioned "__gc" keyword? Does it differ if the
code is compiled with the /clr switch? What, if the above code is written in
a body of a member function within a class that's been marked __gc? Do I
have to put that keyword explicitly?

Thank you for your time,
--
Mehdi

"Richard Grimes [MVP]" <read my sig> wrote in message
news:ui**************@tk2msftngp13.phx.gbl...
Mehdi Mousavi wrote:
Hi folks,
I'm new to Managed C++. I would like to know how can I implement a
function like this:

CWhateverClass **GetPointer()
{
//how can I allocate memory on heap (for my local variable of type
CWhateverClass),
//so that it won't go away when the
//local variable is out of scope.
}
You don't allocate memory, instead, you create objects. If you think in
terms of objects and object references then it makes life far easier. The
nuts and bolts of memory allocation are the responsibility of the GC, not
you!

With that in mind you don't need a pointer to a pointer, instead try this:

CWhateverClass __gc * GetPointer()
{
CWhateverClass __gc * obj = __gc new CWhateverClass;
// stuff
return obj;
}

// other code
CWhateverClass __gc * ptr = GetPointer();
// [1]

The point is that the __gc object is created on the managed heap, and a
*reference* to that object is returned. At the end of the method there are
two references, and once the stack has been cleared the local variable obj
goes out of scope and hence only the reference ptr will exist. However,
since at least one reference to the object exists it means that *if* a
garbage collection occurs at point [1] then the object will not be

removed.
Note that my terminology is a little hazy here because at point [1] both the reference ptr and obj will exist, but the GC will not be able to obtain the reference obj (it will exist somewhere in memory, but the GC will not be
able to access it and so it is 'unreachable') and hence the GC ignores it.

Richard
--
my email ev******@zicf.bet is encrypted with ROT13 (www.rot13.org)
sign up for my free .NET newsletter at
http://www.wd-mag.com/newsletters/

Jul 21 '05 #3

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

Similar topics

3
by: zhphust | last post by:
I want to convert a object of a managed class to a unmanaged structure that has the same member with that managed class. Can anybody tell me how i can do it? Thanks in advance. -- zhphust...
10
by: Bonj | last post by:
I use the following code to convert a managed string to an unmanaged one: _TCHAR error; const char* umstring = (const char*)Marshal::StringToHGlobalAnsi (merror).ToPointer(); _tcscpy(error,...
4
by: William F. Kinsley | last post by:
My understanding is that when I re-compile a existing MFC application with the /clr switch, that the code generated is managed(with some exceptions) but that the data isn't, i.e. not garbage...
2
by: bor_kev | last post by:
Hi, First of all, i want to use the new managed class syntax and STL.NET under Microsoft Visual (C++) Studio 2005 Beta. I read in a Microsoft...
4
by: Maxwell | last post by:
Hello, Newbie question here for disposing of unmanaged resources in MC++.NET. I have a managed VS.NET 2003 MC++ wrapper class that wraps a unmanaged C++ dll. What I am trying to figure out is...
2
by: Mehdi Mousavi | last post by:
Hi folks, I'm new to Managed C++. I would like to know how can I implement a function like this: CWhateverClass **GetPointer() { //how can I allocate memory on heap (for my local variable of...
9
by: raylopez99 | last post by:
microsoft.public.dotnet.languages.vc Please comment on the following code fragment. Note the comments "Why"? Particularly, (1) why does referencing and dereferencing a pointer give the same...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
20
by: =?Utf-8?B?VGhlTWFkSGF0dGVy?= | last post by:
Sorry to bring up a topic that is just flogging a dead horse.... but... On the topic of memory management.... I am doing some file parcing that has to be done as quick as posible, but what I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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
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
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...

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.