• Advertisement

Archived

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

initialising dynamic arrays with an argument

This topic is 5063 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
The following works for me (gcc):

cBlock *m_Blocks = new cBlock[NUMBEROFBLOCKS](m_Display);

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

Right so...I don''t need to delete vectors when I''m finished with them? I think I will be okay now. Thanks for helping.

Share this post


Link to post
Share on other sites
Another way around that would be to make a Create(...) function in your cBlock class and pass any parameters to it.

for example:


m_Blocks = new cBlock[NUMBEROFBLOCKS];

for(int i = 0; i < NUMBEROFBLOCKS; i++)
m_Blocks[i ].Create(m_Display);




[edited by - OneBitWonder on April 15, 2004 9:01:41 PM]

Share this post


Link to post
Share on other sites
//in your headder:

class cBlock
{
//your code...

//my code:

static std::vector<cBlocks*> Blocks;
int InstanceID;
};

//in your codefile:

std::vector<cBlocks*> cBlocks::Blocks;

//in your constructor:

for(int i=0;i<Blocks.size();i++)
if(Blocks[i]==NULL)
{
Blocks[i]=&this;
goto GoOn;
};
Blocks.push_back(&this);
GoOn:
InstanceID = i;
//... continue with your code


//in your destructor:

for(int i=0;i<Blocks.size();i++)
if(Blocks[i]==&this)
Blocks[i]=NULL;
//... continue with your code



You can now access a cBlock-Instance by using
(*cBlock::Blocks[numberOfThatInstance])


Several pro's are given to you by using this technique, ie. you can create cBlocks from anywhere, destruct em from anywhere and access em from anywhere. Also it is fully authomatic done and dynamically generated.


edit: damn html^^


[edited by - fooman on April 16, 2004 3:10:05 AM]

Share this post


Link to post
Share on other sites

  • Advertisement