Dynamic arrays of structures in C++

Started by
9 comments, last by Jx 21 years, 1 month ago
Hi, I came across something today that i can't believe i haven't encountered before... I'm trying to allocate a dynamic array of structures... sounds simple enough. So i have my code that looks something like this:


MyStructure* myStruct;

myStruct = new MyStructure[1024];

  
..but the compiler is complaining that it can't find a default constructor in the structure. Obviously not, because it doesn't have one, but I guess my question is - how do i create a dynamic array of my structures? I have read the forums but alot the answers say to create a default constructor... fine... but what if MyStructure is someone elses code that i can't modify? thanks Jx [edited by - Jx on March 6, 2003 3:50:00 PM]
Advertisement
If there's no default constructor you can allocate the memory and construct each object in the array yourself - using a copy constructor if available, or placement new. You'll also need to take responsibility for properly deallocating memory and calling destructors, which is why you might want to let std::vector do this for you, at least for copy-constructible objects.

A technique that may be easier to work with is to wrap MyStructure in a new class and create a default constructor for that class. You can then allocate an array of the new class instead - but in many cases this won't work, simply because then it won't be an array of MyStructure objects any more...

[edited by - spock on March 6, 2003 4:45:02 PM]
You can use the malloc system call. It''s the old school way to allocate memory in C. Change

myStruct = new MyStructure[1024];
to
myStruct = (MyStructure*)malloc(1024*sizeof(MyStructure));

I thought about using malloc, but i wasn''t sure about the technicalities of using both new and malloc in the same code.

Are they safe to use together?

Jx
No, they are NOT safe to use together for classes (that is, unless you REALLY know what you''re doing). using malloc will not call ANY constructor on the object, so it won''t be properly initialized. Worse yet, it won''t initialize a class'' vtable, so any virtual functions will cause your program to crash.

If you want to pass arguments to each constructor, don''t use the array-new. Just do something like this:

  myStructure **myStruct;myStruct = new myStructure[1024];for(i=0; i<1024; i++)     myStruct[i] = new myStructure("arguments", "to", "constructor");  

Keep in mind that you must refer to members by the arrow rather than the dot operator, and that you must delete each member individually, not using delete[].

But... but that''s what HITLER would say!!
Assuming that MyStructure is a struct, not a class, malloc will not cause any problems. I have never had a problem using new and malloc in the same code, but there could be something i''m missing. Just don''t use malloc for objects. You could put the structure in a wrapper class, but that would add unnecessary overhead.
I am not sure what compiler you are using, but VC7 has no problem with this:

struct mystruct {

int x;
long y;
};


int main() {

mystruct* structList;

structList = new mystruct[100];

delete [] structList;

return 0;
}

classes are in fact implemented as structs by the compiler, with some other "vodoo" going on to ensure the call of constructors etc. There should be no problem with your statements in most modern compilers.

Spock''s idea of wrapping your struct would work, and you would lose very litte "cleanliness" in your code if you were to use an adapter pattern to interface your struct with other objects in the system.

Perhaps the struct is really a class, with a private default constructor? I have seen some code where classes are defined with the struct keyword. Such is life is bizarro world I guess.
============================A guy, some jolt, and a whole lot of code :)
CVertex *p=new CVertex[m_iVertices+1];

the constructor code has not been written yet


class CVertex
{
public:
CVertex(){};
~CVertex(){};
vec3_t p;
float u;
float v;
};

and don t tell me this wouldn t work otherwise my app would crash all the time
http://www.8ung.at/basiror/theironcross.html
quote:Original post by Anonymous Poster
Assuming that MyStructure is a struct, not a class, malloc will not cause any problems. I have never had a problem using new and malloc in the same code, but there could be something i''m missing.

Yes, you''re missing the fact that structs and classes are exactly the same thing. (Except for public/private behaviour). The same problems with using malloc() for classes apply to structs too.
quote:Original post by Anonymous Poster
Assuming that MyStructure is a struct, not a class, malloc will not cause any problems.
Malloc is safe to use with POD types. Not all structs are POD types, and not all classes are not POD types. The standard is quite clear on what a POD type is. However, the original poster''s problem involved an error about not having a default constructor. As such, the type is definitely not a POD type, and malloc alone will not suffice.

This topic is closed to new replies.

Advertisement