Jump to content
  • Advertisement
Sign in to follow this  
RoundPotato

potatoeses or potatoeseses

This topic is 2117 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement

Off the top of my head, try

SomeObject **so;

so = new SomeObject*[max_objects];

An array is a pointer to a block of memory, so in your case you want a pointer to a block of pointers that you can then individually allocate

Share this post


Link to post
Share on other sites

If you want to use any inheritance then you need to story individual pointers to objects, so the 4 bytes are required anyway (and it's not like 4 bytes per object will actually make an impact).  If you only have one object then I'd suggest just giving them a bool active variable and then just setting it to zero to show that they're 'dead' or whatever.  As to the unbearableness, it only affects maybe three lines of your code, and besides, you *are* using C++.  What did you expect?  

 

really though, unless there's some big reason why you're using an array like this, I'd recommend you switch to using a std::vector<SomeObject*>

Share this post


Link to post
Share on other sites

Try

class obj {
    //WE you need here
};

int main() {
    obj *p_obj;

    try {
        p_obj = new obj[3];
    }
    catch (alloc_ex ex) {
        //error handling; or
        return -1;
    }

    //Do Something
    return 0;
}

But notice that if you have any pointers or other specific behavior data inside your class they may need to be initialized.

 

Still, if you are making something really -that- performance intense, you should consider using simple structs and functions that get their pointer to do any manipulation. Object Orientation was not conceived to increase runtime performance, but for the extreme benefits it brings to the architecture and development of the software itself, in terms of development time, reuse, decoupling, [...].

Edited by dejaime

Share this post


Link to post
Share on other sites

If your problem is the constructor call and performance hit, stop using classes.

You could simply use structs and external functions for them. This would probably give you a performance gain by paying the cost of a harder development.

 

The way I understood your first, post the problem was not performance.

 

This may interest you: http://www.boost.org/doc/libs/?view=category_Memory

Specially: http://www.boost.org/doc/libs/1_54_0/libs/pool/doc/html/boost/object_pool.html

 

Boost is open source under a really permissive license.

 

There's never need to reinvent the wheel.

Edited by dejaime

Share this post


Link to post
Share on other sites

If you insist on doing it that way, then malloc is indeed what you want to use if you want to allocate the memory for them without calling the constructors.  You just have to be careful that the first bit of memory in each object doesn't ever end up being zero, otherwise your check wouldn't work.  

 

Alternately, just have an empty constructor.  

Share this post


Link to post
Share on other sites
const size_t BUFFER_LENGTH = MAX_OBJ_COUNT * sizeof(Class);

//allocate a buffer and cast it
char buffer[BUFFER_LENGTH];
Class* objects = (Class*)buffer;

//use placement new
new (objects[0])Class(); //create a Class object at memory location objects[0]
new (objects[1])Class(); //etc - don't call delete on these when you're done with them. Just call their dtors manually.

Better yet, just design the class to have an active/inactive flag and use an object array. Have the class ctor turn the active flag off and do no other work. Set a pointer to the start of the array. When you add an object, activate the object at the pointer position and configure it, or just copy/move it from a configured outside object. Advance the pointer object-by-object until it points to an inactive position, or to the position past the end (indicating that the array is full).

 

Overhead, I am implementing a high-performance library which will be used for extreme tasks and those tasks greatly increase in resource consumption dependant on countless calls to the library. Worse case scenario I would use the pointers, but I hope someone knows of a better solution.

 

 

Sounds like a questionable design, but that's really not much information to go on.

 

Another option is the flyweight pattern.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!