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! ]
|
|
0
|
|
|
|
Reply
|
rohit_dhamija
|
7/19/2004 10:20:49 AM |
|
Hi,
> How can we write our own "new operator" ?
Add the members "operator new" and "operator new[]" to your class, with
matching delete operators.
> 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! ]
|
|
0
|
|
|
|
Reply
|
Thomas
|
7/19/2004 5:28:58 PM
|
|
On 19 Jul 2004 06:20:49 -0400, rohit_dhamija@rediffmail.com (Rohit
Dhamija) wrote:
>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.
>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).
>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).
>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.
>c) Initialize values of the class
Hmmm... that's what a constructor is for...
>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 ?
>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! ]
|
|
0
|
|
|
|
Reply
|
Andrea
|
7/19/2004 8:58:43 PM
|
|
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.
--
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! ]
|
|
0
|
|
|
|
Reply
|
Maciej
|
7/19/2004 8:59:46 PM
|
|
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@no.spam.com> wrote in message news:<cdh654$fvs$1@atlantis.news.tpi.pl>...
> 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! ]
|
|
0
|
|
|
|
Reply
|
rohit_dhamija
|
7/27/2004 11:01:50 AM
|
|
rohit_dhamija@rediffmail.com (Rohit Dhamija) wrote in message news:<e77b983b.0407262111.63564671@posting.google.com>...
> 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! ]
|
|
0
|
|
|
|
Reply
|
rmaddox
|
7/28/2004 2:19:36 AM
|
|
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.
--
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! ]
|
|
0
|
|
|
|
Reply
|
Maciej
|
7/28/2004 2:53:01 AM
|
|
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@no.spam.com> wrote in message news:<ce6cpf$4i9$1@nemesis.news.tpi.pl>...
> 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! ]
|
|
0
|
|
|
|
Reply
|
rohit_dhamija
|
8/1/2004 2:27:58 PM
|
|
|
7 Replies
203 Views
(page loaded in 0.025 seconds)
Similiar Articles: enum and operator++ - comp.lang.c++Incidentally, you can write your operator much, much simpler than using a case ... Write your own "new" operator - comp.lang.c++.moderated ... b) Type cast the memory to ... Overloading operators new and delete - comp.lang.c++.moderated ...Write your own "new" operator - comp.lang.c++.moderated MYArray) + MASK]; ... could do it otherwise by overloading the class operator new ... ... Operator New and Operator ... Writing operator<< for std::vector - comp.lang.c++.moderated ...Simply wrap the vector<X> with your own custom container class and define operator<< for ... Because int is a built-in type, writing all 10 elements with operator [] would ... cannot write std::map via ostream_iterator? - comp.lang.c++ ...... 2,3] etc. Note that even if you try and define your own operator ... from std::pair, and an operator<< to write it to std::ostream. Use the new ... Bitwise operators - comp.lang.fortranSo instead of writing: XOR( a , AND( b, c ) ) I could write something ... If you insist on infix notation, you can define your own operators (note: untested ... System-API to get current process memory usage for C/C++ pgm ...The only thing that could give you a portable solution is to write your own mallocs ... I do this with my own operators new() and delete(). This assumes that you don't ... const char ** syntax question - comp.lang.c++.moderatedWrite your own wrapper that switches depending on the library version (either ... of the program shown below? ... Groups ... as soon as I remove the operator new in ... Using labels as immediate operands in GAS with intel syntax - comp ..._start: # write our string to stdout mov ... When ".intel_syntax" was a new thing, I discovered ... up to the ... crank on your engine and distill your own gas ... does vector::resize throw bad_alloc exception? - comp.lang.c++ ...I think that it is legal to roll up your own allocator ... standard) allocator, that will rely on: void* operator new ... mean, can you conceive of any > possible way to write ... STL allocators, global new/delete using the heap and shared memory ...(where one cannot provide a > new operator in the class). I know its considered bad form to respond to your own post but I have had a response from Scott Meyers that ... Introduction to Computer Programming - Defining Operatorshow to output a value from a procedure that you write; you can write your own operators ... I want a new operator, let's call it randomInRange, which produces an output ... C++ Operator Overloading Guidelines - Computing + Mathematical ...Just write your compound assignment operator implementation, and ... when you are working on your own classes. Binary Arithmetic Operators + - * ... to other, and return a new ... 7/11/2012 7:56:36 AM
|