allocating arrays

hi, I know this is a dumb question, but I haven't programmed in c++ for ages.
any way, I'm working on an SGI machine (irix) and I need to allocate an array dynamicly and i just don't remember how.
I know that regular vars I allocate like: ClassName *var = new ClassName;
What's the right syntax.

Thanks,
Arnon David.
LVL 3
arnondAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nietodCommented:
Real close.  If ArraySize is a numerical expression/value that specifies the size of the array, then do

ClassName *var = new ClassName[ArraySize];
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nietodCommented:
When you delete the array, you must remember to use "delete []" instead of plain old "delete".  (This error might not be detected, but can cause memory leaks and other problems associated with destructors that never get called.)   So you would use

delete [] var;

not

delete var;

If you array needs more advanced features, like the ability to resize dynamically, the ability to insert or delete items (shifting other items around)  consider using the STL vector<> class.

Let me know if you have any questions.
0
arnondAuthor Commented:
nietod, when I compile it the way you suggested I get an error (1304) : "ClassName::~ClassName()" is inaccessible.
Why cann't I access the destructor ?
I'm using the Open Invntor libraries. Cloud it be that the library doesn't allow allocation of arraies ?
      
Arnon David.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

jasonclarkeCommented:
Does the class have a protected destructor or something like that?  That would indicate that the class ClassName is only intended for use by derivation?

Or perhaps the class has its own allocator, a static Create method or something like that?
0
arnondAuthor Commented:
I don't know. The class is part of a library that I use. maybe the library's documentation will say something about it.

Arnon David.
0
jasonclarkeCommented:
Just check first to see if the destructor is public (just look at the header file to see if it is in the public section).  If it is then the suggestions I made above are not the issue.
0
nietodCommented:
Ot sounds like the destructor is private, as jason said.  If that is the case though, it has has nothing to do with arrays.  You would not be able to create a single instance of the object either, like you could not do

ClassName  Var;

either.  Is that the case?
0
arnondAuthor Commented:
I'm not at my SGI machine so I cann't check the header file but i know for sure that I can allocate singel instances.

Arnon David.
0
nietodCommented:
Then perhaps you specified the wrong class name in the source code when you tried to create the array.  Because this "problem" is a mechanism used to prevent the creation of objects, either as single instances or arrays.  
0
arnondAuthor Commented:
I've solved the problem. The correct way to allocte these kind of objects is to first allocate an array of pointers and the allocate the object themselves to the pointes like this:

ClassType **array = new ClassType* [ArraySize];

for (int i=0;i<ArraySize;i++)
{
   array[i] = new ClassType;
}

this is probably done becasue this is an Open Inventor class and the Open Inventor mechanism de-allocates objects without the user knowing so they disable  the direct allocations of arraies so it wouldn't happen thata n object from somewhere inside the array is deleted and mess up the memory.

any way, nietod, your answer was quite helpfull and had put me on the right track.

Thanks and sorry it took so long,
Arnon David.
0
nietodCommented:
>> so they disable  the direct allocations of arraies
The only way to do this is to define a private (or protected) "operator new []" procedure that is a member of this class.  Do they?
0
arnondAuthor Commented:
I don't know, I didn't look at the .h files.
Maybe I'll look at them later.

Arnon.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.