C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Write your own "new" operator

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Rohit Dhamija
Guest





PostPosted: Mon Jul 19, 2004 10:20 am    Post subject: Write your own "new" operator Reply with quote



Dear ALL,

How can we write our own "new operator" ?

e.g. i have a class A and i want to create a object on heap so inspite
of allocating memory using new i want to create my own function to
simulate new operator.

Are following steps sufficient:
a) compute the size of the class using the number of variables
available( But i dnot know in advance how many variables are present
in the class)
b) Type cast the memory to the class
c) Initialize values of the class
And then access the variables of the class by taking a pointer to that
class and access the corresponding values.

Also, correct me in above steps if i am wrong.

Please suggest.

Regards,
Rohit

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Thomas Richter
Guest





PostPosted: Mon Jul 19, 2004 5:28 pm    Post subject: Re: Write your own "new" operator Reply with quote



Hi,

Quote:
How can we write our own "new operator" ?

Add the members "operator new" and "operator new[]" to your class, with
matching delete operators.

Quote:
Are following steps sufficient:
a) compute the size of the class using the number of variables
available( But i dnot know in advance how many variables are present
in the class)
b) Type cast the memory to the class
c) Initialize values of the class
And then access the variables of the class by taking a pointer to that
class and access the corresponding values.

All these steps have to be carried out by the compiler, actually, in a "new
expression" and will call your "operator new" with the proper arguments,
namely the size of the memory chunk to be allocated. Afterwards, the
constructor of the object is called, and it is supposed to initialize the
object correctly. There's no need for casting, and no need to do this
"manually", except for a proper implementation of the constructor and
operator new.

So long,
Thomas




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Andrea Griffini
Guest





PostPosted: Mon Jul 19, 2004 8:58 pm    Post subject: Re: Write your own "new" operator Reply with quote



On 19 Jul 2004 06:20:49 -0400, [email]rohit_dhamija (AT) rediffmail (DOT) com[/email] (Rohit
Dhamija) wrote:

Quote:
Dear ALL,

How can we write our own "new operator" ?

You can do two different things; one is writing a
custom allocator for a specific class so that when
the C++ expression "new MyClass(...)" is evaluated
the custom allocator is called to get the memory;
the other possibility is redefining the global
allocator so that when someone needs memory for
*any* class using "new xxx(...)" that allocator
is called.

Quote:
e.g. i have a class A and i want to create a object on heap so inspite
of allocating memory using new i want to create my own function to
simulate new operator.

I don't understand what you mean with that "so"
before "inspite". To allocate an object on the
heap there's no need to define a custom allocator
(if I understand what you mean with heap).

Quote:
Are following steps sufficient:
a) compute the size of the class using the number of variables
available( But i dnot know in advance how many variables are present
in the class)

Knowing the "variables" defining the class is at
the same time not enough and pointless. It's not
enough because the size of an instance of a class
is *NOT* the sum of the sizes of the "variables" in it.
It's pointless because C++ is happy to tell you what
is the size of a class instance (sizeof operator).

Quote:
b) Type cast the memory to the class

No need to do that. There's not even need to use
typecasts on the pointers to allocate objects on
the heap or defining custom allocators.
All the needed casts are done by the C++ language
itself in the "new" operator.

Quote:
c) Initialize values of the class

Hmmm... that's what a constructor is for...

Quote:
And then access the variables of the class by taking a pointer to that
class and access the corresponding values.

If you're going to fiddle with the values from
the outside, and you don't have even a constructor...
why are you using a class ?

Quote:
Also, correct me in above steps if i am wrong.

Please suggest.

My suggestion is to start with C++ from a good
reading. C++ is an *horrible* language if you
wanna learn it with just exprimentation.

Believe me... it's an ugly monster done that way.

I saw it ;-)

HTH
Andrea

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Maciej Sobczak
Guest





PostPosted: Mon Jul 19, 2004 8:59 pm    Post subject: Re: Write your own "new" operator Reply with quote

Hi,

Rohit Dhamija wrote:
Quote:
Dear ALL,

How can we write our own "new operator" ?

You may find the "Effective C++" by Scott Meyers (in particular, Items
7-10) very informative on this subject.
There are many issues to consider and many pitfalls you have to avoid -
it will certainly be a good idea to learn about them.

--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Rohit Dhamija
Guest





PostPosted: Tue Jul 27, 2004 11:01 am    Post subject: Re: Write your own "new" operator Reply with quote

Thanks all, but i was in process of writing my own memory routines and
wanted to avoid new. And donot wanted to use constructor also....
In that case are my four points to create functinality similar to new
valid ?
Rohit
Maciej Sobczak <no.spam (AT) no (DOT) spam.com> wrote

Quote:
Hi,

Rohit Dhamija wrote:
Dear ALL,

How can we write our own "new operator" ?

You may find the "Effective C++" by Scott Meyers (in particular, Items
7-10) very informative on this subject.
There are many issues to consider and many pitfalls you have to avoid -
it will certainly be a good idea to learn about them.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Randy Maddox
Guest





PostPosted: Wed Jul 28, 2004 2:19 am    Post subject: Re: Write your own "new" operator Reply with quote

[email]rohit_dhamija (AT) rediffmail (DOT) com[/email] (Rohit Dhamija) wrote in message news:<e77b983b.0407262111.63564671 (AT) posting (DOT) google.com>...
Quote:
Thanks all, but i was in process of writing my own memory routines and
wanted to avoid new. And donot wanted to use constructor also....
In that case are my four points to create functinality similar to new
valid ?
Rohit

Since this is pretty much saying "I want to use C++, but not use C++",
why are you using C++?

Since you want to avoid everything C++, you probably want to avoid
using sizeof as well? But that would help with your point 1. Other
than that, you seem to have the gist of it. But what about
destruction? I presume you are as opposed to destructors as to
constructors.

In any case, if you bypass the C++ memory allocation routines, and
bypass the C++ ctor/dtor mechanisms, what you are writing is not C++,
so you really cannot expect much help from the compiler, or from this
newsgroup.

Good luck with it. You may want to think seriously about what you are
trying to accomplish, since C++ already does what you seem to want.

Randy.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Maciej Sobczak
Guest





PostPosted: Wed Jul 28, 2004 2:53 am    Post subject: Re: Write your own "new" operator Reply with quote

Hi,

Rohit Dhamija wrote:
Quote:
Thanks all, but i was in process of writing my own memory routines and
wanted to avoid new.

I do not exactly understand but I suspect that you took it backwards...
Operator new is supposed to get the memory from somewhere - it may take
it from *your* memory routines.

In reality, you *want* to have new, because it is part of the language
and can do a lot of good stuff for you that is related to creation of
objects.
What you probably need is to write your own "memory routines" and have
them used by operator new, so that whenever you write:

A *p = new A;

the memory will come from *your* memory routines, not from whenever the
default implementation of new gets it. This will be hidden from the
programmer - the above line of code does not show where the memory comes
from.

Quote:
And donot wanted to use constructor also....

There is no need to avoid new and constructors. It is like removing
wheels from your car - not a good idea for any real-life purpose.

What exactly do you want to achieve?
Do you want to write your own memory manager and plug it into the
language? Or do you want to invent completely new way of creating objects?
If it is somehow difficult to describe it, please try to first write a
couple of lines that you would like to have working, assuming that
everything is already done. This may help to pin down the goals.


--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Rohit Dhamija
Guest





PostPosted: Sun Aug 01, 2004 2:27 pm    Post subject: Re: Write your own "new" operator Reply with quote

Yes, atually I want to write my own memory manager that should
automatically compute the size of the structure i am defining and
initialize its variable. i will not using the c++ compiler, but want
to write the above functionality. And i suppose this is the
functionality what new does. so in other words i want to simulate new
keyword.
rohit

Maciej Sobczak <no.spam (AT) no (DOT) spam.com> wrote

Quote:
Hi,

Rohit Dhamija wrote:
Thanks all, but i was in process of writing my own memory routines and
wanted to avoid new.

I do not exactly understand but I suspect that you took it backwards...
Operator new is supposed to get the memory from somewhere - it may take
it from *your* memory routines.

In reality, you *want* to have new, because it is part of the language
and can do a lot of good stuff for you that is related to creation of
objects.
What you probably need is to write your own "memory routines" and have
them used by operator new, so that whenever you write:

A *p = new A;

the memory will come from *your* memory routines, not from whenever the
default implementation of new gets it. This will be hidden from the
programmer - the above line of code does not show where the memory comes
from.

And donot wanted to use constructor also....

There is no need to avoid new and constructors. It is like removing
wheels from your car - not a good idea for any real-life purpose.

What exactly do you want to achieve?
Do you want to write your own memory manager and plug it into the
language? Or do you want to invent completely new way of creating objects?
If it is somehow difficult to describe it, please try to first write a
couple of lines that you would like to have working, assuming that
everything is already done. This may help to pin down the goals.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.