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

heap allocating classes

If a class has no constructor/destructor, is not part of an
inheritance heirarchy, is not a template, and only contains members of
integral types, then is it okay to allocate it off the heap? Is this
bad style?

For example:

class myClass {
public:
int myInt;
}
class myClass * pMyClass;

pMyClass = new myClass();
// --OR (in Win32)--
pMyClass = (class myClass *) HeapAlloc(hProcessHeap,
HEAP_ZERO_MEMORY, sizeof(class myClass));
Jul 22 '05 #1
4 3793

"Shailesh Humbad" <s@mailpass.com> wrote in message
news:vb*******************@fe2.columbus.rr.com...
If a class has no constructor/destructor, is not part of an
inheritance heirarchy, is not a template, and only contains members of
integral types, then is it okay to allocate it off the heap? Is this
bad style?
The "goodness/badness" of 'style' really depends upon
your application design.

For example:

class myClass {
public:
int myInt;
}
class myClass * pMyClass;

pMyClass = new myClass();
This is essentially the same as:

pMyClass = new myClass;

But you'll need to remember to delete the object when you're
done with it. Unless you have a compelling reason to dynamically
allocate, I recommend simply defining the object at the necessary
scope, whereupon it will be automatically destroyed without need
for your intervention:

MyClass x;
// --OR (in Win32)--
pMyClass = (class myClass *) HeapAlloc(hProcessHeap,
HEAP_ZERO_MEMORY, sizeof(class myClass));


We don't do Windows (or any other nonstandard code) here.

-Mike
Jul 22 '05 #2
On Tue, 04 May 2004 19:30:35 GMT, Shailesh Humbad <s@mailpass.com> wrote:
If a class has no constructor/destructor, is not part of an
inheritance heirarchy, is not a template, and only contains members of
integral types, then is it okay to allocate it off the heap? Is this
bad style?
Where would you have gotten the impression that it is bad style to allocate
objects on the heap based on the structure of the objects? The choice of
storage class (statically allocated, automatic or from the heap/free store)
has, IMO, little to do with the nature of the object, but everything to do
with what you intend to /do/ with the object(s). For example, prior
knowledge (or lack thereof) of how many objects you'll need might sway the
choice of storage mechanism to use.

For example:

class myClass {
public:
int myInt;
}
class myClass * pMyClass;
pMyClass = new myClass();


Okay, let's stick with the above, and not get into any Windoze nastiness.
What you've shown is perfectly OK, although (a) I'd use a struct in that
case, and (b) I prefer the paren-free syntax:

pMyClass = new myClass;

in order to maintain consistency with the non-dynamic syntax:

MyClass mc; // OK
MyClass mc(); // Bzzzt, C++ pothole of major proportions

-leor

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #3
Leor Zolman wrote:
If a class has no constructor/destructor, is not part of an
inheritance heirarchy, is not a template, and only contains members of
integral types, then is it okay to allocate it off the heap? Is this
bad style?

Where would you have gotten the impression that it is bad style to allocate
objects on the heap based on the structure of the objects? The choice of
storage class (statically allocated, automatic or from the heap/free store)
has, IMO, little to do with the nature of the object, but everything to do
with what you intend to /do/ with the object(s). For example, prior
knowledge (or lack thereof) of how many objects you'll need might sway the
choice of storage mechanism to use.

Thanks for your reply. My question really has to do with the
difference between an allocation function like malloc, HeapAlloc, etc.
and the new operator. Both allocate space for the class object off
the free store or heap. The new operator additionally calls any
constructor defined for the class (and any in the inheritance chain).
That's the only difference I can think of until I read this article,

http://www.codeproject.com/tips/newa...asp?print=true

, which explains that new throws an exception on failure rather than
returning an error code (NULL), and that it's not possible to realloc
space allocated by new. The new operator has always been a little
mysterious to me. There's nothing else going on right?

I would have used a struct, but the members of my class are only to be
accessible by two other classes and nothing else.

Jul 22 '05 #4
On Tue, 04 May 2004 22:52:07 GMT, Shailesh Humbad <s@mailpass.com> wrote:
Leor Zolman wrote:
If a class has no constructor/destructor, is not part of an
inheritance heirarchy, is not a template, and only contains members of
integral types, then is it okay to allocate it off the heap? Is this
bad style?

Where would you have gotten the impression that it is bad style to allocate
objects on the heap based on the structure of the objects? The choice of
storage class (statically allocated, automatic or from the heap/free store)
has, IMO, little to do with the nature of the object, but everything to do
with what you intend to /do/ with the object(s). For example, prior
knowledge (or lack thereof) of how many objects you'll need might sway the
choice of storage mechanism to use.

Thanks for your reply. My question really has to do with the
difference between an allocation function like malloc, HeapAlloc, etc.
and the new operator.


Your next question, you mean? That doesn't sound much like the first one...
Both allocate space for the class object off
the free store or heap.
Right, and the default behavior of new may very well be to use malloc
internally, but code would be non-conformant if it actually assumed that
and attempted to do something that relied upon it (such as free() the
memory.)
The new operator additionally calls any
constructor defined for the class (and any in the inheritance chain).
Well, just think of it as calling "the" constructor for the object being
instantiated (or objects in an array). The fact that the constructor may
call others is somewhat tangential.
That's the only difference I can think of until I read this article,

http://www.codeproject.com/tips/newa...asp?print=true

, which explains that new throws an exception on failure rather than
returning an error code (NULL),
True, but that is an "exceptional" condition; i.e., how out-of-memory is
handled doesn't significantly affect normal performance of the allocation
mechanism, and ought only be an issue with respect to choosing the most
appropriate way in your program for dealing with the possibility of running
out of memory. You can even tell new not to throw an exception (but just
return NULL) using the nothrow syntax, eliminating that as a difference, if
you so desire.
and that it's not possible to realloc
space allocated by new.
Yes, well, realloc is rather quirky in its own right, so no great loss
there.
The new operator has always been a little
mysterious to me. There's nothing else going on right?
I don't know how deep you want to go with that question (not that I know
much more about it than what's already been said), but I'd suggest that
when using C++ in general, you stick with new/delete unless you have some
/very/ good reason to use the *alloc/free instead.

I would have used a struct, but the members of my class are only to be
accessible by two other classes and nothing else.


And you've declared them public? :-)
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #5

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

Similar topics

14
by: Kevin Grigorenko | last post by:
Hello, I couldn't find an obvious answer to this in the FAQ. My basic question, is: Is there any difference in allocating on the heap versus the stack? If heap or stack implementation is not...
17
by: Jonas Rundberg | last post by:
Hi I just started with c++ and I'm a little bit confused where stuff go... Assume we have a class: class test { private: int arr; };
11
by: Dan Elliott | last post by:
Hello all, I am writing a program which needs to run as quickly as possible, but holds a lot of data in memory (around 1GB for a usual run). Is this too much memory to even consider putting...
2
by: Nick McCamy | last post by:
I have a question related to allocating on the stack. In this program below, are my following assumptions true? - variable a is allocated on the heap since it's static - variable b is...
2
by: Ganesh | last post by:
I am not sure if this question is relevant here. Why is 'heap' (the place where dynamic memory allocation occurs) called so? There is also a data structure called 'heap'. What is the connection? ...
15
by: Joe Van Dyk | last post by:
Can someone explain what a heap and what a stack is? And why I should care? I used to know, but then I forgot. And I can't seem to find it in the C++ FAQ. I keep reading how allocating from...
16
by: sarathy | last post by:
Hi all, I need a few clarifications regarding memory allocaion in C++. I apologize for the lengthy explanation. 1. In C++, Objects are allocated in heap. What does heap refer to? Is it an area...
53
by: fdmfdmfdm | last post by:
This is an interview question and I gave out my answer here, could you please check for me? Q. What are the memory allocation for static variable in a function, an automatic variable and global...
4
by: =?Utf-8?B?VGhlTWFkSGF0dGVy?= | last post by:
I am very sorry to bring a topic up that has been beaten with an ugly stick..... but... If I want to "fix" an byte array for the life time of a program would it be better allocating it on the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.