0

I have a class A whose objects are created dynamically:

A *object;
object = new A;

there will be many objects of A in an execution. i have created a method in A to return the address of a particular object depending on the passed id.

A *A::get_obj(int id)

implemetation of get_obj requires itteration so i chose vectors to store the addresses of the objects.

vector<A *> myvector

i think another way to do this is by creating a file & storing the address as a text on a particular line (this will be the id). this will help me reduce memory usage as i will not create a vector then. what i dont know is that will this method consume more time than the vector method? any other option of doing the same is welcome.

Cool_Coder
  • 4,888
  • 16
  • 57
  • 99
  • If you create a huge vector and only use part of it, the operating system will probably write the unused parts of it to file and load them when they are needed, so don't be afraid of making large data structures you use only part of. Look up paging. – Daniel Oct 26 '11 at 07:20
  • 2
    Unless the file is cached in memory some way, file access is always several magnitudes slower than memory access. The only reason to use a temporary file is if the platform has severely limited memory and you really need to save every last byte you can. – Some programmer dude Oct 26 '11 at 07:21
  • What is the size of the data set and on which platform do you intend to run the code? – FireAphis Oct 26 '11 at 07:31
  • size is around 1GB (total execution of exe) & on windows XP – Cool_Coder Oct 26 '11 at 07:35

3 Answers3

5

Don't store pointers in files. Ever. The objects A are taking up more space than the pointers to them anyway. If you need more A's than you can hold onto at one time, then you need to create them as needed and serialize the instance data to disk if you need to get them back later before deleting, but not the address.

MartyTPS
  • 530
  • 2
  • 5
  • +1: This warning should be heeded. Although it is fine if it is a temporary file that is only valid while the application runs. – Björn Pollex Oct 26 '11 at 07:29
3

will this method consume more time than the vector method?

Yes, it will consume a lot more time - every lookup will take several thousand times longer. This does not hurt if lookups are rare, but if they are frequent, it could be bad.

this will help me reduce memory usage

How many object are you expecting to manage this way? Are you certain that memory-usage will be a problem?

any other option of doing the same is welcome

These are your two options, really. You can either manage the list in memory, or on disk. Depending on your usage scenario, you can combine both methods. You could, for instance, keep frequently used objects in memory, and write infrequently used ones out to disk (this is basically caching).

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
  • thanks...i am a beginner so i dont know what u r talking abt writing to disk, but about memory i think u r reffering to `new`...am i correct? – Cool_Coder Oct 26 '11 at 07:30
  • @CAD_coding: *Writing to disk* means storing things in a file. Storing things in memory can be done by using `new`, but it seems that you should take the time to read a basic tutorial or, even better, read a [good book](http://stackoverflow.com/questions/388242/). – Björn Pollex Oct 26 '11 at 07:35
2

Storing your data in a file will be considerably slower than in RAM.

Regarding the data structure itself, if you usually use all the ID's, that is if your vector usually has empty cells, then std::vector is probably the most suitable approach. But if your vector will have many empty cells, std::map may give you a better solution. It will consume less memory and give O(logN) access complexity.

The most important thing here, imho, is the size of your data set and your platform. For a modern PC, handling an in-memory map of thousands of entries is very fast, but if you handle gigabytes of data, you'd better store it in a real on-disk database (e.g. MySQL).

FireAphis
  • 6,650
  • 8
  • 42
  • 63
  • thanks, i am using c++ & dont know MySQL... could u tell me how to _store it in a real on-disk database_ using c++ – Cool_Coder Oct 26 '11 at 07:34
  • @CAD_coding: How to create a database is probably worth a separate question, however the fact that you plan to store pointers worries me... – FireAphis Oct 26 '11 at 07:43