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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
nietodConnect With a Mentor Commented:
Real close.  If ArraySize is a numerical expression/value that specifies the size of the array, then do

ClassName *var = new ClassName[ArraySize];
0
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
All Courses

From novice to tech pro — start learning today.