dynamic array with pointer

N

Nils

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
 
R

Rob Williscroft

Nils wrote in
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.
 
L

lilburne

Nils said:
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.
 
N

Nils

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 said:
Nils wrote in
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.
 
R

Rob Williscroft

Nils wrote in
Hi,
[top rearranged]

Rob Williscroft said:
Nils wrote in
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.
 
R

Rolf Magnus

Nils said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,147
Messages
2,570,834
Members
47,382
Latest member
MichaleStr

Latest Threads

Top