By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,336 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

dynamic array with pointer

P: n/a
Hi,
I want to create a dynamic array with pointer, without allocation of the
memory.
I tried it so:

objekt **ob= new objekt[size];

It is not working, because he will parameter for the Konstructor ob objekt.
It works in the following way

objekt **ob= (object**) new DWORD[size];

but I get _CrtCheckMemory errors, althought all datas in the array work all
right.
Can anybody help me please, Thank you
Nils
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Nils wrote in news:bp**********@ariadne.rz.tu-clausthal.de:
Hi,
I want to create a dynamic array with pointer, without allocation of
the memory.
I tried it so:

objekt **ob= new objekt[size];
objekt *ob= new objekt[size];

Later do:

delete [] ob;

It is not working, because he will parameter for the Konstructor ob
objekt. It works in the following way

objekt **ob= (object**) new DWORD[size];

but I get _CrtCheckMemory errors, althought all datas in the array
work all right.


If you need more help then you should say what you are trying to do.
Its not clear why you are writing objekt **ob above, is this a 2D
array or it objekt a base class and you want an array of derived
objects.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2

P: n/a
Nils wrote:
Hi,
I want to create a dynamic array with pointer, without allocation of the
memory.
I tried it so:

objekt **ob= new objekt[size];

It is not working, because he will parameter for the Konstructor ob objekt.
It works in the following way

objekt **ob= (object**) new DWORD[size];

but I get _CrtCheckMemory errors, althought all datas in the array work all
right.
Can anybody help me please, Thank you


I think what you are tring to say is that you want a dynamic
array capable of handling 'size' objects, but you don't want
all that memory allocated immediately, nor the objekt ctor
being called. Probably your best bet would be to use
std::vector< wrapped_ptr<objekt> >. Where wrapped_ptr is
your favourite smart pointer implementation.

Jul 22 '05 #3

P: n/a
Hi,

I use ** because I alreade have so Objects, so I don't want to have new
memory allocated. I need the array as a kind of a "hashtable" for the
pointers to the Object. I think I found a way to do it:

Objekt **ob=new ob*[size];

But thought an array should be faster, if I know the position where the
information is, isn't it?

Thanks for your Help!!

"Rob Williscroft" <rt*@freenet.REMOVE.co.uk> schrieb im Newsbeitrag
news:Xn**********************************@195.129. 110.201...
Nils wrote in news:bp**********@ariadne.rz.tu-clausthal.de:
Hi,
I want to create a dynamic array with pointer, without allocation of
the memory.
I tried it so:

objekt **ob= new objekt[size];


objekt *ob= new objekt[size];

Later do:

delete [] ob;

It is not working, because he will parameter for the Konstructor ob
objekt. It works in the following way

objekt **ob= (object**) new DWORD[size];

but I get _CrtCheckMemory errors, althought all datas in the array
work all right.


If you need more help then you should say what you are trying to do.
Its not clear why you are writing objekt **ob above, is this a 2D
array or it objekt a base class and you want an array of derived
objects.

Rob.
--
http://www.victim-prime.dsl.pipex.com/

Jul 22 '05 #4

P: n/a
Nils wrote in news:bp**********@ariadne.rz.tu-clausthal.de:
Hi,
[top rearranged]
"Rob Williscroft" <rt*@freenet.REMOVE.co.uk> schrieb im Newsbeitrag
news:Xn**********************************@195.129. 110.201...
Nils wrote in news:bp**********@ariadne.rz.tu-clausthal.de:
> Hi,
> I want to create a dynamic array with pointer, without allocation of
> the memory.
> I tried it so:
>
> objekt **ob= new objekt[size];
objekt *ob= new objekt[size];

Later do:

delete [] ob;
>
> It is not working, because he will parameter for the Konstructor ob
> objekt. It works in the following way
>
> objekt **ob= (object**) new DWORD[size];
>
> but I get _CrtCheckMemory errors, althought all datas in the array
> work all right.


If you need more help then you should say what you are trying to do.
Its not clear why you are writing objekt **ob above, is this a 2D
array or it objekt a base class and you want an array of derived
objects.

I use ** because I alreade have so Objects, so I don't want to have new
memory allocated. I need the array as a kind of a "hashtable" for the
pointers to the Object. I think I found a way to do it:

Objekt **ob=new ob*[size];

I think you mean

Objekt **ob=new Objekt *[size];

If you already have the pointers then this is correct, but please
consider,

#include <vector>

std::vector< Objekt * > ob;

in a function use:

ob.resize( size );

for ( unsigned position = 0; position < size; ++position )
{
ob[ position ] = Objekt_pointer_for_position;
}

std::vector will manage the memory for you no need to call delete []
later.
But thought an array should be faster, if I know the position where the
information is, isn't it?


Not sure what you mean here, but an array or std::vector will let you
find a pointer from its index (position) in a single operation.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #5

P: n/a
Nils wrote:
Hi,

I use ** because I alreade have so Objects, so I don't want to have
new memory allocated. I need the array as a kind of a "hashtable"
for the pointers to the Object. I think I found a way to do it:

Objekt **ob=new ob*[size];

But thought an array should be faster, if I know the position where
the information is, isn't it?


That depends on what you want to do and how the data itself is arranged.
I mean, if you want an array of pointers that point to the objects, you
already must have some type of container that contains those objects
themselves.

Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.