Thread: Database project to practice pointers

  1. #1
    Registered User
    Join Date
    May 2020
    Posts
    13

    Database project to practice pointers

    I want to start a new C project that will help me better understand pointers and memory management. I've been using languages like Java and PHP, so I never had to deal with concepts like pointers, manual memory management or anything low level. The only way I could compare pointers in C are references in Java when you create an object of a class, the object is pointing to the memory address of that class, but I'm not even sure if i'm right. So I came up with a project that will help me understand how pointers are used, so came up with this database project.



    This is the plan for my database project (basically the description I wrote all down):



    "The database (which is basically a text file) will contain the following with the following data type: firstname (char[]), lastname (char[]), id (int), photo (int). For the photo, what you want to do is obtain the binary representation of any image, then store that in the text file along with the other information, as specified below:
    firstname//lastname//idnum//photo
    fname//lname//12//0110101011001100101...
    fname2//lname2//22/010101011011010001101...




    Each new information added to the database text file starts a newline towards the end of the binary representation of the image.


    users will be prompted to input each information. For the photo part, what you want to do is prompt the user with the option to upload an image file (‘y’ or ‘n’). If user types in ‘y’, open the file directory, check if what the user selects is an image file, and if it is, obtain the binary representation of the image file and store it along with the other information with the format as specified above. Then what you want to do is add another prompt where the user will have to type in their information, then the program will loop through the database text file and obtain the information as specified by the user input and output it to the terminal as shown below:
    Code:
    Hello fname lname. You're ID is idnum
    The binary image will be used for the GUI version

    Afterwards, you want create a GUI version of the command line version above using GTK, and the GUI will consist of input fields that will allow users to input their information, as well as the button that will allow users to upload an image and obtain the binary representation of that image and store everything on a text file. Then in the GUI, it will also contain an input where users will be asked to input their firstname, lastname and id, then the program will search through the file containing the input information, then output everything, including the image (using the binary representation to output the image) in a grid format, like shown below:
    Code:
    [photo]: idnum
    Full name: fname lname
    "

    My question is, what instances will I have to use pointers in this database project? BTW, i'm doing this project on Ubuntu.

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    212
    How many images? And what size?

    My first thoughts would be that you want to keep the image data in a seperate file to the metadata about the image.

    Treating the image data as a heap would help you learn a lot about how they work. Knowing how heaps work is very helpful for C programming.

  3. #3
    Registered User
    Join Date
    May 2020
    Posts
    13
    Quote Originally Posted by hamster_nz View Post
    How many images? And what size?

    My first thoughts would be that you want to keep the image data in a seperate file to the metadata about the image.

    Treating the image data as a heap would help you learn a lot about how they work. Knowing how heaps work is very helpful for C programming.
    Only one image, and the size depends on whether it's a jpg or png file. If I were to obtain the binary representation of the image, would I use dynamic memory allocation (storing the binary into an array, then storing the array information in with the other input data in the text file)?

    The only thing I know about stack and heap is that variables are added to the stack while class objects are added to the heap

  4. #4
    Registered User
    Join Date
    Sep 2020
    Posts
    63
    Wheb hamster_nz spoke about heaps he porbbly meant the data structure, not the place where malloc allocates the memory.
    Heap Data Structure - GeeksforGeeks

    I am not sure if a text file is the best way to store image data.
    Wouldn't be a real database like SQLite or MySQL on option?

  5. #5
    Registered User
    Join Date
    May 2020
    Posts
    13
    Quote Originally Posted by thmm View Post
    Wheb hamster_nz spoke about heaps he porbbly meant the data structure, not the place where malloc allocates the memory.
    Heap Data Structure - GeeksforGeeks

    I am not sure if a text file is the best way to store image data.
    Wouldn't be a real database like SQLite or MySQL on option?
    It's not actually a database. It's a "pseudo" database where information will be stored in a text file, then the program will later loop through the file and display what the information added to the file. The image file would be for later on when building a GTK version of this mini project where the image file will be displayed along with the rest of the information stored in the file.

    And I just thought of this project to be familiar with pointers and memory management since I haven;t been using those features ever since learning C.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,594
    I would suggest you make version 1 just be "firstname//lastname//idnum"

    > Each new information added to the database text file starts a newline towards the end of the binary representation of the image.
    Binary image files will contain every single possible byte value.
    You would need to do something like base64 encoding of the image to reliably mark the end of the data with a newline.
    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.

  7. #7
    Registered User
    Join Date
    Sep 2020
    Posts
    212
    I was actually meaning a "C malloc()" type heap, but in a file. With blocks maybe like this

    Code:
    Signature        Maybe "IMGE" or any other useful tag.
    Size of block    32-bit int including these fields
    Size of data     32 bit int, this is just the image data.
    Image data       binary, padded to next 32-bit boundary
    Size of block    32 bit int
    That way, in your main database 'table' you can just store the address of the Signature, and then extract the original binary from the file.

    When you delete an image, just set the blocks 'size of data'' to zero

    When you add an image, you just write a new block at the end of the file.

    Eventually when your needs get sophisticated enough you can merge 'unused' blocks, or keeping a list of free blocks that can be reused.

    Because of the size being stored immediately prior to the next block, it is possible to walk the data in both directions, adding and removing things using only local information.

    This is pretty much how malloc() and free() work.

    Oh, adding file locks would be a good idea too.

    Darn. Now I want to implement this!
    Last edited by hamster_nz; 11-09-2020 at 03:49 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Practice project ideas for beginners? allegro?
    By Bodach in forum C++ Programming
    Replies: 4
    Last Post: 03-29-2012, 09:31 AM
  2. c my project to connect to the database
    By Polat1256 in forum C Programming
    Replies: 4
    Last Post: 05-19-2010, 04:00 PM
  3. Database project proposal
    By MPSoutine in forum Windows Programming
    Replies: 1
    Last Post: 12-22-2003, 12:50 PM
  4. Project database
    By mdshort in forum C++ Programming
    Replies: 3
    Last Post: 12-09-2003, 06:19 AM
  5. Hey I fished the small little practice project
    By incognito in forum C Programming
    Replies: 3
    Last Post: 03-05-2003, 08:36 PM

Tags for this Thread