Link to home
Start Free TrialLog in
Avatar of arnond
arnond

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nietod
nietod

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.
Avatar of arnond

ASKER

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.
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?
Avatar of arnond

ASKER

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.
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.
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?
Avatar of arnond

ASKER

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.
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.  
Avatar of arnond

ASKER

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.
>> 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?
Avatar of arnond

ASKER

I don't know, I didn't look at the .h files.
Maybe I'll look at them later.

Arnon.