I want to make a program in C++ that reads a file where each field will have a number before it that indicates how long it is.
The problem is I read every record in object of a class; how do I make the attributes of the class dynamic?
For example if the field is “john” it will read it in a 4 char array.
I don’t want to make an array of 1000 elements as minimum memory usage is very important.
In order to do this, you need to use dynamic allocation (either directly or indirectly).
If directly, you need
new[]anddelete[]:If you are allowed to use boost, you can simplify that a bit by using
boost::shared_array<>. With a shared_array, you don’t have to manually delete the memory as the array wrapper will take care of that for you:Finally, you can do dynamic allocation indirectly via classes like
std::stringorstd::vector<char>.