Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Jaggy

initialising dynamic arrays with an argument

This topic is 5331 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

hey there, I''m sure this question is really easy but I can''t seem to find a solution anywhere. I''m creating a dynamic array of a number of blocks like this: m_Blocks = new cBlock[NUMBEROFBLOCKS]; but the problem is that I need to pass an argument to each of the constructors, in this case a pointer to my display. If I was just creating one block I would do it like this: m_Blocks = new cBlock(m_Display); but for arrays, do I put the argument after the [] or before or where? See! Very easy Thank you if you can help me. I''m using vc++ 6.0.

Share this post


Link to post
Share on other sites
Advertisement
You can''t do that in standard C++ and I don''t believe that any of MSVC 6''s extensions will allow it either.

One work around is to use a std::vector<cBlock>, and use the resize() function to insert values into the vector.

Share this post


Link to post
Share on other sites
quote:
Original post by Jaggy
do I put the argument after the [] or before or where?


I''m not being helpful, but anyway...

Two things:

1. Why don''t you use vectors?
2. Why don''t you just try and see if before or after works, instead of asking?

Oh, crap SiCrane beat me to it. oh well.

Share this post


Link to post
Share on other sites

I did try both before and after before coming here, but they both errored. I figured it might have been something dumb like I needed some weird character between them.

And vectors - I haven''t used them before. I''ll take a look, though. Thank you.

Share this post


Link to post
Share on other sites
That''s a non-standard gcc extension. If you compile that with -pedantic, it will give you an error.

Share this post


Link to post
Share on other sites
I played around with vectors, and I did get it working So thanks.
This is how I did it, please let me know if I'm doing anything bad:

In my header file I declared the collection of blocks like this:


std::vector<cBlock*> m_EditBlocks;


and filled it like this:

m_EditBlocks.resize(NUMBEROFBLOCKS, new cBlock(m_Display));


That works great. I have a problem now which is deallocating the memory.
I have used

delete [] m_EditBlocks;


but it errors. Which got me thinking I have been using my vector wrongly. Can anyone help?

edit : lost some code in the HTML

[edited by - jaggy on April 15, 2004 9:29:01 AM]

Share this post


Link to post
Share on other sites
Why use a cBlock* at all?
Why not just a vector<cBlock>?
That''ll take care of all the memory management for you.

Share this post


Link to post
Share on other sites
This poses an interesting question, how DO vectors allocate memory? For example, when you resize or push_back, does it reallocate the whole vector or work like a linked list, assigning new memory all over the place and linking them in each element.

What I''m asking is, does a vector work like an array, and if so, does a sizeof( vectorOfInts[0] ) == sizeof( int ) ?

Stay Clausal,

Red Sodium

Share this post


Link to post
Share on other sites
Vectors use continues memory, AFAIK. It uses an std::allocator to allocate the memory. The allocator probably reserves 2mb of data, and the vector takes what it needs. If it needs more, the allocator already have memory reserved, and it uses that memory.

I assume, that when the allocator runs out of memory, it will allocate a new block, and move all other data into that.

Toolmaker



My site
/* -Earth is 98% full. Please delete anybody you can.*/

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!