Memory, how do you manage?

hi people of cplusplus

i am still a new learner of the language C. have yet to dive into C++. i have written one or two programs. in this semester of my studies, i learn data structures.

before this, all my temporary large variables are all stored into a .txt file (using fprintf and fscanf). now that i've learned linked lists, i learned another way of storing these large variables.

for example, a program to store all the names of the folder in a directory. which would you prefer? port to .txt? linked lists? or do you have any way of your own? discuss :D
In programming there are many ways to solve the same general problem, so the chosen implementation is decided by each specific situation.

If you need the list to be kept from the last session each time you start the program, a file is the only approach (although, unless the list is really huge, you'd still use a temporary container). Otherwise, this would be a waste of time as file IO is really slow.

If you want quick insertions and don't care about memory use and random access, you need a linked list.

If you don't care about insertion times (or know how many elements you'll have beforehand) and want more efficient memory use and random access, you need a vector (It's a dynamic array).

If you want the list sorted, to be able to quickly check if some X is in it or not, you could use a binary tree.
@hamsterman

Ever heard of databases?
Last edited on
@rapidcoder

That's a bit besides the point. Unless you're going to do something else than "read old, write new", a text file is just as good as a database (I'd even say better). This topic should be about data structures.

@OP

Like hamsterman said, there are millions of different data structures. Each and every one of them has a purpose and each one has been designed to excel at a specific task (e.g. Quick search, quick sort, quick min/max, quick check-if-exists, etc).

Generally, there's no reason to write anything to a text file during the program. Input and output can be from files, but as temporary storage you should use variables rather than writing to disk.
Depends on how much data you have. If it doesn't fit in memory, you need to use databases. Often a database can do in a few minutes what would take a week to code in C++ and another week to run.
Last edited on
Since the OP discusses the advantages over a Linked List versus temporary file storage, I'd say size isn't the problem.
@hamsterman
thanks for your opinion

@rapidcoder
If it doesn't fit in memory, you need to use databases

sorry to ask, is it possible to implement a database in C/C++? i've learned database before with all the oracle and sql... but with C?

@gaminic
Each and every one of them has a purpose and each one has been designed to excel at a specific task

indeed. very true.

Since the OP discusses the advantages over a Linked List versus temporary file storage

its not restricted to these two. my preference is file i/o, thought admittedly slower, but i am most familiar with it. just learned basic data structures this year. wanted to know how others think about their memory management :)
Generally, you're going to use an outside database and access it through C++. (I've never done it; it was terrible enough in Java, and C++ doesn't seem to make it easier.)

Generally, you should never read from or write to files during execution. The time required to read a variable (even from a very large object) is insignificant compared to the cost of reading from the disk.

If you're interested in data structures, you should definitely try implementing some. Heaps and Search Trees are very fun programming exercise with incredible practical relevance.
Topic archived. No new replies allowed.