Thread: a problem with a memory management

  1. #1
    Registered User
    Join Date
    Apr 2008

    a problem with a memory management


    I decided to program a cd database application with a binary file database. With this app i can add to and remove cds from the database, edit information in the database, view the cds in the database and search information from the database.

    I'm a beginner and i have done only few little programming projects. Now i'm a bit lost how i shoud implement the memory management for this app. Should i use a linked list here? What's the professional way to do this? Thanks a lot!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Start with a fixed length array. It will allow you to get a lot of the logic working correctly without complicating the logic.

    Changing to a variable length array using allocated memory will be a lot easier afterwards.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Quote Originally Posted by Salem View Post
    Start with a fixed length array. It will allow you to get a lot of the logic working correctly without complicating the logic.
    Thanks for the reply. Do you mean that, if i have a database which consist of an array with eg. 200 structs in it, i should allocate memory to an array for an array with 500 structs what's the max length of my array? I mean in which parts of the program i should do the memory allocation? Is it just before i start to read data from the file to the memory?

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Visalia, CA, USA
    This falls slightly outside the realm of your initial question, but I remember using this in a project I did about five years ago. Its called Gracenote these days (and looks like a pain in the neck to use unlike its past incarnations).

    And as far as your question to Salem goes, get your code working within a very controlled environment, then start doing the complicated stuff. Far too often people try to sort out their memory manager while writing their code. Not knowing the effectiveness of their code, nor their memory manager, debugging the code becomes a grueling nightmare. So keep things simple. And as you perfect one area of your program, then work on the other.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    So to clarify Salem's and Master5001's comments:
    If you have a
    struct CDEntry {
       int  id;     // Unique identifier in the database. 
       char title[somenumber];
       char artist[someothernumber];
       .... // More stuff here, that I'm sure you know better than I do. 
    Then make this a global variable (yes, I know global variables are evil, but in this case there will only be ONE database, and making it global should be OK):
    #define MAXCDS 200
    struct CDEntry cddatabase[MAXCDS];
    Then work out how you store your CD's to a file, enter new data into the database, find a CD by title, artist etc, delete entries, export as Excel spreadsheet, etc, etc.

    For the future:
    If you want to really do this the DATABASE way, then the artists should be in a "artists" table, the CD entries in a "cd" table, and you probably would need a few more tables (such as "label"). But now you need a proper (relational) database engine to solve the problem.

    On the other hand, that's like building a top-league (F1, Indycar, WTCC or World Rally Series) race-car, when you have no experience whatsoever in building/modifying cars - it would be much better to start by building something like a gocart, because with suifficient DIY skills, that CAN BE DONE - so start with a basic database design that you can add, delete and search. Once you have a fair understanding of how it should work, it becomes a much easier task to take it to the next level (which of course is far from the TOP LEVEL).

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Join Date
    Oct 2007
    Inside my computer
    It's also a good idea to prefix it, something like:
    struct CDEntry g_cddatabase[MAXCDS];
    So you know it's a global variable. It's always good to keep track of their visibility (so you don't get confused as to where they are defined) and to avoid hiding variables in the outer scopes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with atl memory cleaning
    By Mariam1989 in forum Windows Programming
    Replies: 1
    Last Post: 11-11-2008, 12:35 PM
  2. Problem with linked list and shared memory
    By Sirfabius in forum C Programming
    Replies: 10
    Last Post: 11-10-2008, 04:45 PM
  3. Memory management
    By CompiledMonkey in forum C++ Programming
    Replies: 9
    Last Post: 12-19-2003, 11:41 AM
  4. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM
  5. Memory Management
    By rasdfasfd in forum C Programming
    Replies: 2
    Last Post: 12-03-2002, 08:57 PM