473,659 Members | 2,488 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1953
* 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_p tr 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
2240
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 device. There is a program available to enumerate PCI devices and give the address of the registers assigned during the POST of the PC. That info can be a manual input at program
1
1500
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 but I can't seem to get my Google-fu working well enough to find it.
124
4911
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 RealPlayer, MediaPlayer and all applications that need speed are written in c++ ? (except for
6
6858
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 and page clean percentages, as well as an average of transaction rate for the entire days work. The ORDERST_BP was created to service our most active table, and I've been assuming that the low hit ratio for this BP is what is
3
3500
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
5720
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 collected. I also noticed that when replaced some of the existing MFC dialogs with managed winforms that everything is still running in the same app domain.( No context change) So my question is this, what are the performance differences in using...
12
3086
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 asp editing (like I used to be able to do with 2K/IIS5) where I use VS.NET, open an asp file, make changes, save and refresh my browser. Problem is that I get an Access is Denied error when I try to save the file and then the file gets wiped on...
11
1527
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 m_objCMember As CMember Public Function addMember(ByVal objMember As CMember) m_objCMember = objMember
8
6377
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 Distance across all records and offer the top ten matches. Unfortunately this is very slow on long strings. Is there a faster method to do this?
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8751
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8539
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7360
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6181
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4342
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2759
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.