Subject: Use of new and delete in C++

E

Eric A. Johnson

Hi,

I am working on a way to create a "window" within a dos console. I'm
using Microsoft Visual C++. My main problem is that I am using new to
create a new CHAR_INFO array within the constructor, and I want to destroy
it within the destructor using delete. However, the array is not known
outside of the constructor. This is a class within a class. Until the
object is created, it has no way to know the size of the array; the size is
passed as a parameter to the constructor. Is there a way to make the
destructor aware of the array, or to make it public to the class from within
the constructor? I am using it to store the original information of the
screen that will be overwritten, so that when the window is finished, it can
rewrite the original information (including the background and foreground
color) before the "window" is completely destroyed.

On a side note, has anybody else written advanced console applications? How
did you handle "windows"? If I can get this to work properly, I plan to,
eventually, use it to create menus and so on within the console.

Thanks in Advance,
Eric
 
V

Victor Bazarov

Eric said:
I am working on a way to create a "window" within a dos console. I'm
using Microsoft Visual C++. My main problem is that I am using
new to create a new CHAR_INFO array within the constructor, and I
want to destroy it within the destructor using delete. However, the
array is not known outside of the constructor. This is a class
within a class. Until the object is created, it has no way to know
the size of the array; the size is passed as a parameter to the
constructor. Is there a way to make the destructor aware of the
array, or to make it public to the class from within the constructor?

Make your array (pointer) a member. Initialise it in the constructor,
delete it in the destructor. It seems so straightforward that I am
thinking there is a catch in your explanation, which I've missed.

What book are you reading to understand dynamic memory better?
I am using it to store the original information of the screen that
will be overwritten, so that when the window is finished, it can
rewrite the original information (including the background and
foreground color) before the "window" is completely destroyed.
On a side note, has anybody else written advanced console
applications? How did you handle "windows"? If I can get this to
work properly, I plan to, eventually, use it to create menus and so
on within the console.

It doesn't really seem relevant where the dynamic memory is used or
what it represents.

V
 
E

Eric A. Johnson

Victor Bazarov said:
Make your array (pointer) a member. Initialise it in the constructor,
delete it in the destructor. It seems so straightforward that I am
thinking there is a catch in your explanation, which I've missed.
You're right... it was straightforward... I'm just trying to learn so much
at one time, that I guess I overlooked that. Thanks for the tip.
What book are you reading to understand dynamic memory better?
"C++ From the Ground Up", by Herbert Schildt... is he any good? I have two
C++ books by him. If he isn't good, can you recommend a really good book /
author?
 
P

Phlip

Eric said:
You're right... it was straightforward... I'm just trying to learn so much
at one time, that I guess I overlooked that. Thanks for the tip.
"C++ From the Ground Up", by Herbert Schildt... is he any good? I have two
C++ books by him. If he isn't good, can you recommend a really good book /
author?

Please hit:

http://groups-beta.google.com/groups?as_q=schildt&as_ugroup=comp.lang.c++

This group tends to dislike Schildt. He's a very good writer for warm
friendly prose, and very bad for technical details or thoroughness.

Go ahead and learn from the book, but make sure you read many more. These
can't hurt:

Accelerated C++
The C++ Programming Language, 3rd Edition

Others will recommend more.
 
P

Peter Koch Larsen

Eric A. Johnson said:
Hi,

I am working on a way to create a "window" within a dos console. I'm
using Microsoft Visual C++. My main problem is that I am using new to
create a new CHAR_INFO array within the constructor, and I want to destroy
it within the destructor using delete.

You should use a std::vector (or perhaps a std::string) for this purpose.
However, the array is not known outside of the constructor. This is a
class within a class. Until the object is created, it has no way to know
the size of the array; the size is passed as a parameter to the
constructor. Is there a way to make the destructor aware of the array, or
to make it public to the class from within the constructor? I am using it
to store the original information of the screen that will be overwritten,
so that when the window is finished, it can rewrite the original
information (including the background and foreground color) before the
"window" is completely destroyed.

As Victor Bazarov suggested, it is possible, but beware that you also must
create a correct copy-constructor now. It is much easier just to use the
std:: containers mentioned above.

Peter
[snip]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,662
Latest member
sxarexu

Latest Threads

Top