handling dynamic array

Hello,

I have the following problem.I try to implement a template similar to the STL's vector template(I could use the STL,but at the moment I would like to learn some of templates,so that's why I decided to implement it on my own).Now I can make a piece of this type with the constructor that I wrote,and it works fine for basic C types(so, char,int,double and float),but unfortunatelly It probably won't work for other types(mainly classes),as I allocate the space for my array by malloc,however I know it would work with new and delete,I still wouldn't like to use these functions,as unfortunatelly they don't have something like realloc,which is a very good feature of malloc type functions.Actually the first data of my class is a pointer with the template's type,I give the convenient value for it by calling malloc in the consturctor,but provided I call the template with a class naturally it doesn't work,as I should use new or do something else.Could you recommend me some options how to solve the problem without new?I wouldn't like to use it,as I have a function,like push_back in the vector type,it uses a realloc now,but with new I guess it would need too much memory space without really need it.I was thinking about to call the destructor and constructor after allocating manually,but how could I decide if the T template parameter is class or not?

Thanks for your help in advance,

Gábor Jenei
Get over it. Use new. And look at how vector grows its storage.

What do you think is so great about realloc? It is basically {malloc new memory; copy old to new; free old;} Yes, it can occasionally grow an already allocated memory block. But my experience has been that it usually doesn't. Granted it has be decades since I actually used it.
I guess you could use placement new to call a class's ctor, and then explicitly call the dtor before you free the memory. (You'd have to do this with the "new" approach too since you'd need to be able to delete individual elements and not the full container)

This will let you use malloc and free for memory allocation / release.

HOWEVER

This won't work with realloc because as PanGalactic mentions, realloc basically just memcpy's over the data, which can UTTERLY DESTROY class contents which is why you should never do it. So realloc is not going to work with any approach (except with POD types).

EDIT: actually, memcpy could even screw POD types up if they point to themselves:

1
2
3
4
5
6
7
8
9
10
struct foo
{
  int a;
  int* ptr;
};

foo bar;
bar.ptr = &bar.a;

// if 'bar' gets memcpy'd, you're hosed 


/EDIT

So yeah... if you want non POD types, you CAN'T use realloc.

And if you're not using realloc, then there's no reason not to use new/delete.

So yeah. Get over it. Shake the malloc/free bad habit.
Last edited on
So,I should do the following thing: allocate memory for my array by new in the constructor,and deallocate it by delete,and when I want to call the function to extern or reduce the array I should first allocate an array with the new size by new again,the copy the first n elements of the old array,and ad mine,at the end I should delete the old array.Was it the method that you recommended?I thought there would be a more effective solution which does not need so much memory,actually if my old array was n length,I need 2n+1 memory space in the heap.Isn't it too much?Or it doesn't count too much?
That's what std::vector needs. You won't find a more optimal solution because if one was known
to exist, std::vector would use it instead.
Topic archived. No new replies allowed.