473,396 Members | 2,020 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,396 software developers and data experts.

Quick and dirty C++ managed memory

Why not have the "new" operator return a typed handle when you get
memory on the heap (if you want it to)? And then use a "lock" operator
to get a pointer to the memory, and the pointer is only valid within
the scope of the function. Then on idle time, all memory is unlocked
and can be compacted.

I wonder if there is a way of doing this by overiding the global new
and delete operators and somehow checking for handle types?

In any case I think it would be better if the compiler was in on the
action.

For instance you could declare a handle:

char* # hString;

hString = new char[80];

if( hString )
{
char* pString = lock hString;

strcpy( pString, "A String");

int len = strlen( pString );
}

And when you want to free the memory:

delete [] hString;
And when calling a member function:

CThisObject* # hThis = new CThisObject( data );

if( hThis )

hThis->DoThat();
is the same as:

CThisObject* # hThis = new CThisObject( data );

if( hThis )
{
CThisObject* p = lock hThis;

p->DoThat();
}

In any case, I'm sure die-hard C++ programmers would prefer managed
memory along these lines instead of all the crap you have to deal with
in .NET managed C++.

Jul 23 '05 #1
5 1933
* countzero:
Why not have the "new" operator return a typed handle when you get
memory on the heap (if you want it to)?
It returns a typed pointer.

And then use a "lock" operator to get a pointer to the memory,
Locking is a good idea for systems without hardware memory management;
in C++ it can be implemented as a smart-pointer (no language extension
necessary).

and the pointer is only valid within the scope of the function.


That's a std::auto_ptr.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2
On Fri, 11 Mar 2005 06:30:49 GMT, al***@start.no (Alf P. Steinbach)
wrote:
* countzero:
Why not have the "new" operator return a typed handle when you get
memory on the heap (if you want it to)?


It returns a typed pointer.


Thanks for "pointing" out the painfully obvious.

But can you comprehend the concept of a typed handle that can point to
different memory addresses except when its "locked"?
And then use a "lock" operator to get a pointer to the memory,


Locking is a good idea for systems without hardware memory management;
in C++ it can be implemented as a smart-pointer (no language extension
necessary).


Actually when it comes to Java, C# and Visual Basic the memory is
somewhere along the lines directly poked and prodded. And during idle
time it's compacted with (likely) hardware memory management.

Having a version of C++ that directly accesses the memory from handles
during an event-drivien, non-idle function is no different from those
languages.

"Locking" within an event-function is just a possible term that gets
the current address of the memory from the handle. In that context,
Java, C# and Visual Basic all "lock" memory then.

As for smart-pointers they don't do the same job and aren't as simple.

I want typed handles to objects allocated on the heap that can change
address. And I want to know they are handles in the code and the
compiler will give me errors if I don't use them the right way.
and the pointer is only valid within the scope of the function.


That's a std::auto_ptr.


There you go. It's theoretically possible.

But like I said I don't want all the technical garbage collection, er,
garbage...

I want to eliminate memory fragmentation in the most simplest way. And
typed handles to memory objects that can be moved around are, by far,
the most simplest way for a C++ programmer.

Jul 23 '05 #3

Alf P. Steinbach wrote:
* countzero:
Why not have the "new" operator return a typed handle when you get
memory on the heap (if you want it to)?
It returns a typed pointer.

And then use a "lock" operator to get a pointer to the memory,


Locking is a good idea for systems without hardware memory

management; in C++ it can be implemented as a smart-pointer (no language extension necessary).

and the pointer is only valid within the scope of the function.


That's a std::auto_ptr.


No. Not at all. It's not a handle nor a lock. std::auto_ptr cannot,
by any definition of the word, move the allocated memory. Any
reference to the object must remain valid. With a lock(handle),
the raw pointer returned is usable for the duration of the lock.
When the lock goes out of scope, pointers and references are
invalidated but the object itself remains valid. (although it
could move). If an auto_ptr goes out of scope, the object is
destroyed.

It is possible to write a standard C++ class which has the desired
handle behavior, and a lock class. The lock can probably be
boost::scoped_ptr with a custom 'deleter'. When the lock goes out
of scope, the object can be moved again.

HTH,
Michiel Salters

Jul 23 '05 #4
* msalters:

Alf P. Steinbach wrote:
* countzero:
Why not have the "new" operator return a typed handle when you get
memory on the heap (if you want it to)?


It returns a typed pointer.

And then use a "lock" operator to get a pointer to the memory,


Locking is a good idea for systems without hardware memory
management;
To repeat myself: Locking is a good idea for systems without hardware memory
management.

and the pointer is only valid within the scope of the function.


That's a std::auto_ptr.


No. Not at all. It's not a handle nor a lock. std::auto_ptr cannot,
by any definition of the word, move the allocated memory.


It doesn't need to under common operating systems; the OS does that for you;
also, see above.

E.g., in old Windows and on the Mac you had to do explicit locking, nowadays
the OS does that and moves memory blocks all the time without affecting the
logical addresses in a process.

Cheers,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #5
countzero wrote:
"Locking" within an event-function is just a possible term that gets
the current address of the memory from the handle. In that context,
Java, C# and Visual Basic all "lock" memory then.

As for smart-pointers they don't do the same job and aren't as simple.
They can do the same job and can be as simple. You just have to write one.
I want typed handles to objects allocated on the heap that can change
address. And I want to know they are handles in the code and the
compiler will give me errors if I don't use them the right way.


And why do you think it's not possible to write a smart pointer that does
that?

Jul 23 '05 #6

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

Similar topics

0
by: Tom | last post by:
I have made in the past a bunch of VB apps to test hardware. They work and some call functions written in C++ (dll calls). I need to make a quick, dirty and cheap console app to access a PCI...
1
by: andy | last post by:
What's the easiest way for a script to throw up simple dialogs such as file select and alerts? I thought there was something in the standard library that didn't need TK and was cross-platform...
124
by: 43 | last post by:
how come m$Office isn't written in .net? how come Open Office isn't written in j2ee? how come dbms systems aren't written in either? how come browsers aren't written in either? how come...
6
by: Sean C. | last post by:
Helpful folks, I am having a hard time figuring out how to reduce my percentage of dirty page steal activity. Below are statistics for three fairly normal days, with the bufferpool hit ratios...
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...
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...
12
by: Ron Weldy | last post by:
I have a test server runinng 2003/IIS 6 with a mixture of asp and asp.net files. On my workstation I have a share set up to the folder where the web files reside. I am just doing quick and dirty...
11
by: Dmitry | last post by:
Hi there, Just came across this problem and was wondering if someone can shed a light on it as it somewhat puzzles me. Suppose I have the following classes: Public Class CTest Private...
8
by: Martin Schneider | last post by:
Hi! I have a database with approx. 2500 records. When entering a new record I'd like to avoid double entries that may differ slightly in writing. Currently I am calculating the Levenshtein...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.