Transforming Vector to a Dynamic Array

Hello :)
I am a C++ newbie and I am working on my "Driver's Book" project for my university. I have a complete code done (With export to CSV, HTMl...all that is needed. A hours of work for newbie like me) with a vector, but my teacher said he would like to see a use of dynamically allocated array instead of vector. And i am LOST. Is there any way to rewrite use of vector in my code :

 
vector <item> book;


to a dynamically allocated array, with only a few changes in my code instead of rewriting the whole thing :(. I am really desperate in this case and i need your help guys.

Thanks in advance,
Oli
Your teacher is either an evil programmer, a low-level programmer, or a sadist. >_>

We'll need to know more about your code and what features of vector you're using to be able to answer your questions about what needs changing and how to change it.

-Albatross
Thank you Albatross for your quick response.
Indeed, he is evil...and he is WELL known for it.

My project is all around keeping database of driving. Storing info about each drive - date, time, fuel consumption, fuel price (To be specific = 6 values per one drive) Number of drives are undeclared and each drive can be added and specified by user. With ability to export and load data from classic CSV files and exporting to a HTML (Parts I understand pretty well).

The only role of the vector as you probably already know is to store the data. So that why i need a dynamic array with ability to change its properties according to number of drives stored by user. Rest of the code is pretty straight-forward.

What i need is information how to declare such an array (i have NO IDEA) and store/read data from it.
I can post here the whole code, but problem is that is full of notes and names in my native language, but i can translate it in english if it's necessary.

Good thing that i still have a lot of time to finish this project, but i don't really know where to start.

Thanks :)
-Oli
Last edited on
Aish, then you're in for some "fun".

Basically, the way one deals with dynamic arrays is that one maintains a pointer to memory that the program has allocated on the heap.

Let's consider the basics:
int* ptr = new int;

What this code does is dynamically allocate an integer, and store a pointer to it in ptr. You can now access that integer whenever you'd like just by dereferencing the pointer.
1
2
*ptr = 3;
std::cout << *ptr;
3


However, you always need to remember to maintain a pointer to that memory. Otherwise, it's a memory leak: memory is being allocated but never freed, which freeing memory manually is something also need to do. Just as you got that memory from the system, you also need to return it to the system when you're done.

delete ptr;

Now, if you want to make an array, the syntax is a bit different...

1
2
3
bool* hassyrup = new bool[3];
//Do stuff.
delete [] bool;


If you're creating an array of objects, then bear in mind that the previous two syntaxes will call the default constructor. If you want to call a non-default constructor (this only works intuitively for non-array allocations), you'll need the following syntax:

AClass* classi = new AClass(7, "duck", "goose");

Resizing is where it starts getting painful. When you want to "resize" a dynamic array, what you have to do is actually this:
1. Allocate another array and maintain a temporary pointer to it.
2. Copy over the contents of the first array to the second.
3. Delete the first array.
4. Set your non-temporary pointer to the temporary pointer's value.

Good luck.

-Albatross
Thank you again Albatross.
Really helpful information. I think i understand it a little bit more now :)
Do you have any piece o code or link to a code that actually uses this type of array in practice ? (Like Address Book, Phone Book type of program..whatever) It would really help me a lot to take a look at a source that actually implement this type of array.

Any piece of code that show example of this array in a case similar to mine would be amazing :)

Thanks
-Oli

EDIT: Still lost, after 6 hours of enormous trying ( and depression :D) still can't get it to work sadly, looks like i need to rewrite the whole code again :/. I would really like to see a similar program...but can't find anything :/
Last edited on
Topic archived. No new replies allowed.