Search code examples
c++arrayspointersmemoryallocation

How to dynamically allocate memory on andress whom I point to?


Hell'o I want to create my own dynamic array (vector) class, but don't know how to allocate memory on addres whom I point to. In function add I added line like: int * object = new (this->beginning + this->lenght) int (paramValue); But visual studio shows me an error message "operator new cannot be called with the given arguments". How to make it works, which arguments should I send to the new operator?


Solution

  • (I am not sure to understand your question, but....)

    You might want to use the placement new operator (but to implement a <vector> like thing you don't need that). Then you'll need to #include <new>

    But you probably don't need that. Just call plain new from your constructor, and plain delete from your destructor. Something like int*arr = new int[length]; (in constructor) and later delete[] arr; (in destructor).

    (it looks that you are misunderstanding something; I recommend spending several days reading a good C++ programming book)