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

Always create the object in heap?



In a recent techincal interview on C++, I came across this strange
question....

How to ensure that your class object is always created in heap (by a
new operator)? The compiler should throw error, if the object is
created in stack.

Note:- Dont hide the construtor.

Is it possible?

Aug 8 '06 #1
13 7241
* Karthik:
>
In a recent techincal interview on C++, I came across this strange
question....

How to ensure that your class object is always created in heap (by a
new operator)? The compiler should throw error, if the object is
created in stack.

Note:- Dont hide the construtor.

Is it possible?
A short answer is to make the destructor inaccessible.

But there are details, e.g. how to allow to 'delete', and then how to
ensure the client code uses smart pointers (if that is desirable).

See section 1.1.3 and following sections of <url:
http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf>.

--
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?
Aug 8 '06 #2
Karthik posted:
>

In a recent techincal interview on C++, I came across this strange
question....

How to ensure that your class object is always created in heap (by a
new operator)? The compiler should throw error, if the object is
created in stack.

Note:- Dont hide the construtor.

Is it possible?

I think they do something like the following. (I don't know how to get it
to compile...):

class MyClass {
private:

~MyClass() {}

public:

void operator delete(void *const parg)
{
::delete static_cast<MyClass*>(parg);
}
};

int main()
{
MyClass *const p = new MyClass;

delete p;
}

--

Frederick Gotham
Aug 8 '06 #3
* Frederick Gotham:
Karthik posted:
>>
In a recent techincal interview on C++, I came across this strange
question....

How to ensure that your class object is always created in heap (by a
new operator)? The compiler should throw error, if the object is
created in stack.

Note:- Dont hide the construtor.

Is it possible?


I think they do something like the following. (I don't know how to get it
to compile...):

class MyClass {
private:

~MyClass() {}

public:

void operator delete(void *const parg)
{
::delete static_cast<MyClass*>(parg);
}
};

int main()
{
MyClass *const p = new MyClass;

delete p;
}
No, 'operator delete' is a deallocation function, not an override of the
keyword 'delete'. See the reference I gave elsethread. Even if it was
written by myself... ;-)

--
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?
Aug 8 '06 #4
Karthik wrote:
In a recent techincal interview on C++, I came across this strange
question....

How to ensure that your class object is always created in heap (by a
new operator)? The compiler should throw error, if the object is
created in stack.

Note:- Dont hide the construtor.

Is it possible?
See this FAQ:

http://www.parashift.com/c++-faq-lit...html#faq-16.21

It hides the constructor, but that is the most common way to do it.
It's silly to leave the constructor public but have it throw a run-time
error. Always prefer compile-time errors when you can get them.

Cheers! --M

Aug 8 '06 #5
Frederick Gotham wrote:
>
class MyClass {
private:

~MyClass() {}

public:

void operator delete(void *const parg)
{
::delete static_cast<MyClass*>(parg);
}
};
It would be good if you called the destructor
inside operator delete.
--
<\___/>
/ O O \
\_____/ FTB. For email, remove my socks.

In science it often happens that scientists say, 'You know
that's a really good argument; my position is mistaken,'
and then they actually change their minds and you never
hear that old view from them again. They really do it.
It doesn't happen as often as it should, because scientists
are human and change is sometimes painful. But it happens
every day. I cannot recall the last time something like
that happened in politics or religion.

- Carl Sagan, 1987 CSICOP keynote address

Aug 8 '06 #6

"Alf P. Steinbach" <al***@start.nowrote in message
news:4j************@individual.net...
>* Karthik:
>>
In a recent techincal interview on C++, I came across this strange
question....

How to ensure that your class object is always created in heap (by a
new operator)? The compiler should throw error, if the object is
created in stack.

Note:- Dont hide the construtor.

Is it possible?

A short answer is to make the destructor inaccessible.

But there are details, e.g. how to allow to 'delete', and then how to
ensure the client code uses smart pointers (if that is desirable).

See section 1.1.3 and following sections of <url:
http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf>.
I take it you mean section 1.3.3?

-Howard
Aug 8 '06 #7

"mlimber" <ml*****@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Karthik wrote:
>In a recent techincal interview on C++, I came across this strange
question....

How to ensure that your class object is always created in heap (by a
new operator)? The compiler should throw error, if the object is
created in stack.

Note:- Dont hide the construtor.

Is it possible?

See this FAQ:

http://www.parashift.com/c++-faq-lit...html#faq-16.21

It hides the constructor, but that is the most common way to do it.
It's silly to leave the constructor public but have it throw a run-time
error. Always prefer compile-time errors when you can get them.
I don't see your point. The requirement was that the "compiler should throw
error, if the object is created in stack", which means a compile-time error.

-Howard


Aug 8 '06 #8
fungus posted:
Frederick Gotham wrote:
>>
class MyClass {
private:

~MyClass() {}

public:

void operator delete(void *const parg)
{
::delete static_cast<MyClass*>(parg);
}
};

It would be good if you called the destructor
inside operator delete.

Does the global operator delete not do that?

(I don't know much about overloading "new" and "delete"... I don't think I've
ever overloaded them.)

--

Frederick Gotham
Aug 8 '06 #9
Frederick Gotham wrote:
fungus posted:
>Frederick Gotham wrote:
>>>
class MyClass {
private:

~MyClass() {}

public:

void operator delete(void *const parg)
{
::delete static_cast<MyClass*>(parg);
}
};

It would be good if you called the destructor
inside operator delete.


Does the global operator delete not do that?
It should. Any overloaded operator delete is the *deallocation* function,
not the "delete operator" that is supposed to destroy the object[s] and
then deallocate. It's a case of bad naming, but "delete operator" calls
"operator delete". What we are allowed to overload is the latter, not
the former.
(I don't know much about overloading "new" and "delete"... I don't
think I've ever overloaded them.)
Nothing to it, really.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 8 '06 #10
Victor Bazarov posted:
>(I don't know much about overloading "new" and "delete"... I don't
think I've ever overloaded them.)

Nothing to it, really.

Any decent tutorials on the net?

Any time I look for a programming tutorial on the net, I get some programming
magazine article written by a pretentious, poor programmer. It can hard to
sort the good stuff from the bad stuff if you don't know the subject
material.

--

Frederick Gotham
Aug 8 '06 #11
Victor Bazarov wrote:
>>It would be good if you called the destructor
inside operator delete.

Does the global operator delete not do that?

It should.
My bad.
--
<\___/>
/ O O \
\_____/ FTB. For email, remove my socks.

In science it often happens that scientists say, 'You know
that's a really good argument; my position is mistaken,'
and then they actually change their minds and you never
hear that old view from them again. They really do it.
It doesn't happen as often as it should, because scientists
are human and change is sometimes painful. But it happens
every day. I cannot recall the last time something like
that happened in politics or religion.

- Carl Sagan, 1987 CSICOP keynote address

Aug 8 '06 #12
* Frederick Gotham:
Victor Bazarov posted:
>>(I don't know much about overloading "new" and "delete"... I don't
think I've ever overloaded them.)
Nothing to it, really.


Any decent tutorials on the net?
See the reference I gave elsethread.

More generally, see the FAQ item "What other "newbie" guides are there
for me?", currently at <url:
http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.21>.

--
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?
Aug 8 '06 #13
Howard wrote:
"mlimber" <ml*****@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Karthik wrote:
In a recent techincal interview on C++, I came across this strange
question....

How to ensure that your class object is always created in heap (by a
new operator)? The compiler should throw error, if the object is
created in stack.

Note:- Dont hide the construtor.

Is it possible?
See this FAQ:

http://www.parashift.com/c++-faq-lit...html#faq-16.21

It hides the constructor, but that is the most common way to do it.
It's silly to leave the constructor public but have it throw a run-time
error. Always prefer compile-time errors when you can get them.

I don't see your point. The requirement was that the "compiler should throw
error, if the object is created in stack", which means a compile-time error.
Mea culpa. I missed the word "compiler" in concentrating too much on
the word "throw."

In any case, I think the factory function approach is preferable over
the private destructor approach since there are fewer things to
remember and more forward compatibility (e.g., what if auto_ptr is
deprecated and replaced with unique_ptr as Howard Hinnant has
proposed?).

Cheers! --M

Aug 8 '06 #14

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

Similar topics

2
by: brazilnut52 | last post by:
I am going to outline the steps I go through to produce the problem. Hopefully this will help you understand the problem better I have created a simple COM DLL in .NET by using the COM class...
2
by: sonu | last post by:
I am tring to create activexobject like var Fo =new ActiveXObject("Scripting.FileSystemObject"); but i am finding error that automation server cant create object any help
1
by: kathyk | last post by:
Hi, I created an application in MS Access 2000. Since upgrading to MS Access 2003 I have been finding all sorts of strange things. My current problem is with the function below. Ig get an error...
2
by: Susan Bricker | last post by:
Hi. I have a routine (BldEmail) that is causing an error on my user's PC but not on mine. HELP!!! I have to fix this tonight. She reports an error with err.number=429 and the error message...
3
by: Ben Lam | last post by:
I read on some message board that i can't find anymore saying that the Large Object Heap is compacted in Framework 1.1 or 2.0. Is this true? I can't seem to find any ms documentation that said...
4
by: sandeep | last post by:
When we use STL which memory space it will use whither it is stack or heap or data segment How to make STL to create in heap? How to make whole container to create in heap? I think container...
5
by: Atmapuri | last post by:
Hi! If the array is allocated from the large object heap does that mean that it does not have to be pinned down when passed to unmanaged code? Is the limit of 1000 elements as the switching...
3
by: Michael | last post by:
Hi. I have COM+ component installed on my XP. I have problem to create instance of it inside vbscript. How can I do it? The error I get is : ActiveX component can't create object. Thanks
0
by: Cainnech | last post by:
Hi all, For my work I need to create a jpeg-file with Photoshop. In order to maintain the same Name-format I decided to create an application in VB6. That way I'm sure that the names always stay...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.