Giter Site home page Giter Site logo

lcthw-ex-17's Introduction

lcthw-ex-17

Extended code of Exercise 17 from the "Learn C the Hard Way" book.

This example turned into a proof-of-concept project that represents a serverless database (similar to the way SQLite works). It reads and writes data to a binary file.

The first bytes of the file store configurations for the database and are always ommitted first when read and write operations against the database are performed. The metadata contains the maximum number of rows the database can have and the maximum length in bytes of the emails and names of the records. There is a hard limit for the size of the name and email data that is specified in preprocessor constants and is 512 bytes.

I have added a BASH shell script for performing automated testing in the utils/ directory.

I have added a CMake config for generating Code::Blocks project files.

You can generate the project files for the Debug and Release modes respectively:

cmake -G "CodeBlocks - Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug
cmake -G "CodeBlocks - Unix Makefiles" -DCMAKE_BUILD_TYPE=Release

lcthw-ex-17's People

Contributors

cvetomird91 avatar

Watchers

 avatar  avatar

lcthw-ex-17's Issues

Dynamic database size

  • add the max_rows and max_data fields to the Database struct (possibly as a nested struct)
  • if the mode is 'create' set the max_data and max_rows fields to the Database struct instance in Database_create function
  • Set read the max_data and max_rows fields in the Database struct member in the Connection struct in the Database_open function
  • start reading database records after skipping the initial bytes in which the max rows and max data values are stored

Rows cannot be more than 100

[cvetomir@localhost ex-17]$ ./utils/test-cases.sh 
==11538== Memcheck, a memory error detector
==11538== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==11538== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
==11538== Command: ./ex17 db.dat c 101 512
==11538== 
==11538== Invalid write of size 8
==11538==    at 0x400D79: Database_create (db_operations.c:155)
==11538==    by 0x40113B: main (main.c:38)
==11538==  Address 0x52193c0 is 0 bytes after a block of size 103,200 alloc'd
==11538==    at 0x4C2DB9D: malloc (vg_replace_malloc.c:299)
==11538==    by 0x400AD0: Database_open (db_operations.c:98)
==11538==    by 0x401084: main (main.c:18)
==11538== 
==11538== Invalid write of size 8
==11538==    at 0x400DB6: Database_create (db_operations.c:155)
==11538==    by 0x40113B: main (main.c:38)
==11538==  Address 0x52193c8[cvetomir@localhost ex-17]$ ./utils/test-cases.sh 
==11538== Memcheck, a memory error detector
==11538== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==11538== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
==11538== Command: ./ex17 db.dat c 101 512
==11538== 
==11538== Invalid write of size 8
==11538==    at 0x400D79: Database_create (db_operations.c:155)
==11538==    by 0x40113B: main (main.c:38)
==11538==  Address 0x52193c0 is 0 bytes after a block of size 103,200 alloc'd
==11538==    at 0x4C2DB9D: malloc (vg_replace_malloc.c:299)
==11538==    by 0x400AD0: Database_open (db_operations.c:98)
==11538==    by 0x401084: main (main.c:18)
==11538== 
==11538== Invalid write of size 8
==11538==    at 0x400DB6: Database_create (db_operations.c:155)
==11538==    by 0x40113B: main (main.c:38)
==11538==  Address 0x52193c8 is 8 bytes after a block of size 103,200 alloc'd
==11538==    at 0x4C2DB9D: malloc (vg_replace_malloc.c:299)
==11538==    by 0x400AD0: Database_open (db_operations.c:98)
==11538==    by 0x401084: main (main.c:18)
==11538== 
==11538== Invalid read of size 8
==11538==    at 0x4EB1930: fseek (fseek.c:35)
==11538==    by 0x400C0E: Database_write (db_operations.c:133)
==11538==    by 0x401147: main (main.c:39)
==11538==  Address 0x8 is not stack'd, malloc'd or (recently) free'd
==11538== 
==11538== 
==11538== Process terminating with default action of signal 11 (SIGSEGV): dumping core
==11538==  Access not within mapped region at address 0x8
==11538==    at 0x4EB1930: fseek (fseek.c:35)
==11538==    by 0x400C0E: Database_write (db_operations.c:133)
==11538==    by 0x401147: main (main.c:39)
==11538==  If you believe this happened as a result of a stack
==11538==  overflow in your program's main thread (unlikely but
==11538==  possible), you can try to increase the size of the
==11538==  main thread stack using the --main-stacksize= flag.
==11538==  The main thread stack size used in this run was 8388608.
==11538== 
==11538== HEAP SUMMARY:
==11538==     in use at exit: 107,880 bytes in 5 blocks
==11538==   total heap usage: 5 allocs, 0 frees, 107,880 bytes allocated
==11538== 
==11538== 8 bytes in 1 blocks are still reachable in loss record 1 of 5
==11538==    at 0x4C2DB9D: malloc (vg_replace_malloc.c:299)
==11538==    by 0x400CA0: Database_create (db_operations.c:144)
==11538==    by 0x40113B: main (main.c:38)
==11538== 
==11538== 24 bytes in 1 blocks are still reachable in loss record 2 of 5
==11538==    at 0x4C2DB9D: malloc (vg_replace_malloc.c:299)
==11538==    by 0x400AAA: Database_open (db_operations.c:95)
==11538==    by 0x401084: main (main.c:18)
==11538== 
==11538== 552 bytes in 1 blocks are still reachable in loss record 3 of 5
==11538==    at 0x4C2DB9D: malloc (vg_replace_malloc.c:299)
==11538==    by 0x4EA904C: __fopen_internal (iofopen.c:69)
==11538==    by 0x400B10: Database_open (db_operations.c:107)
==11538==    by 0x401084: main (main.c:18)
==11538== 
==11538== 4,096 bytes in 1 blocks are definitely lost in loss record 4 of 5
==11538==    at 0x4C2DB9D: malloc (vg_replace_malloc.c:299)
==11538==    by 0x4EA84A1: _IO_file_doallocate (filedoalloc.c:101)
==11538==    by 0x4EB7975: _IO_doallocbuf (genops.c:398)
==11538==    by 0x4EB42E2: _IO_file_seekoff@@GLIBC_2.2.5 (fileops.c:1031)
==11538==    by 0x4EB2003: rewind (rewind.c:35)
==11538==    by 0x400CE7: Database_create (db_operations.c:148)
==11538==    by 0x40113B: main (main.c:38)
==11538== 
==11538== 103,200 bytes in 1 blocks are still reachable in loss record 5 of 5
==11538==    at 0x4C2DB9D: malloc (vg_replace_malloc.c:299)
==11538==    by 0x400AD0: Database_open (db_operations.c:98)
==11538==    by 0x401084: main (main.c:18)
==11538== 
==11538== LEAK SUMMARY:
==11538==    definitely lost: 4,096 bytes in 1 blocks
==11538==    indirectly lost: 0 bytes in 0 blocks
==11538==      possibly lost: 0 bytes in 0 blocks
==11538==    still reachable: 103,784 bytes in 4 blocks
==11538==         suppressed: 0 bytes in 0 blocks
==11538== 
==11538== For counts of detected and suppressed errors, rerun with: -v
==11538== ERROR SUMMARY: 28 errors from 4 contexts (suppressed: 0 from 0)
./utils/test-cases.sh: line 9: 11538 Segmentation fault      (core dumped) valgrind --leak-check=full --show-leak-kinds=all ./ex17 db.dat c 101 512
 is 8 bytes after a block of size 103,200 alloc'd
==11538==    at 0x4C2DB9D: malloc (vg_replace_malloc.c:299)
==11538==    by 0x400AD0: Database_open (db_operations.c:98)
==11538==    by 0x401084: main (main.c:18)
==11538== 
==11538== Invalid read of size 8
==11538==    at 0x4EB1930: fseek (fseek.c:35)
==11538==    by 0x400C0E: Database_write (db_operations.c:133)
==11538==    by 0x401147: main (main.c:39)
==11538==  Address 0x8 is not stack'd, malloc'd or (recently) free'd
==11538== 
==11538== 
==11538== Process terminating with default action of signal 11 (SIGSEGV): dumping core
==11538==  Access not within mapped region at address 0x8
==11538==    at 0x4EB1930: fseek (fseek.c:35)
==11538==    by 0x400C0E: Database_write (db_operations.c:133)
==11538==    by 0x401147: main (main.c:39)
==11538==  If you believe this happened as a result of a stack
==11538==  overflow in your program's main thread (unlikely but
==11538==  possible), you can try to increase the size of the
==11538==  main thread stack using the --main-stacksize= flag.
==11538==  The main thread stack size used in this run was 8388608.
==11538== 
==11538== HEAP SUMMARY:
==11538==     in use at exit: 107,880 bytes in 5 blocks
==11538==   total heap usage: 5 allocs, 0 frees, 107,880 bytes allocated
==11538== 
==11538== 8 bytes in 1 blocks are still reachable in loss record 1 of 5
==11538==    at 0x4C2DB9D: malloc (vg_replace_malloc.c:299)
==11538==    by 0x400CA0: Database_create (db_operations.c:144)
==11538==    by 0x40113B: main (main.c:38)
==11538== 
==11538== 24 bytes in 1 blocks are still reachable in loss record 2 of 5
==11538==    at 0x4C2DB9D: malloc (vg_replace_malloc.c:299)
==11538==    by 0x400AAA: Database_open (db_operations.c:95)
==11538==    by 0x401084: main (main.c:18)
==11538== 
==11538== 552 bytes in 1 blocks are still reachable in loss record 3 of 5
==11538==    at 0x4C2DB9D: malloc (vg_replace_malloc.c:299)
==11538==    by 0x4EA904C: __fopen_internal (iofopen.c:69)
==11538==    by 0x400B10: Database_open (db_operations.c:107)
==11538==    by 0x401084: main (main.c:18)
==11538== 
==11538== 4,096 bytes in 1 blocks are definitely lost in loss record 4 of 5
==11538==    at 0x4C2DB9D: malloc (vg_replace_malloc.c:299)
==11538==    by 0x4EA84A1: _IO_file_doallocate (filedoalloc.c:101)
==11538==    by 0x4EB7975: _IO_doallocbuf (genops.c:398)
==11538==    by 0x4EB42E2: _IO_file_seekoff@@GLIBC_2.2.5 (fileops.c:1031)
==11538==    by 0x4EB2003: rewind (rewind.c:35)
==11538==    by 0x400CE7: Database_create (db_operations.c:148)
==11538==    by 0x40113B: main (main.c:38)
==11538== 
==11538== 103,200 bytes in 1 blocks are still reachable in loss record 5 of 5
==11538==    at 0x4C2DB9D: malloc (vg_replace_malloc.c:299)
==11538==    by 0x400AD0: Database_open (db_operations.c:98)
==11538==    by 0x401084: main (main.c:18)
==11538== 
==11538== LEAK SUMMARY:
==11538==    definitely lost: 4,096 bytes in 1 blocks
==11538==    indirectly lost: 0 bytes in 0 blocks
==11538==      possibly lost: 0 bytes in 0 blocks
==11538==    still reachable: 103,784 bytes in 4 blocks
==11538==         suppressed: 0 bytes in 0 blocks
==11538== 
==11538== For counts of detected and suppressed errors, rerun with: -v
==11538== ERROR SUMMARY: 28 errors from 4 contexts (suppressed: 0 from 0)
./utils/test-cases.sh: line 9: 11538 Segmentation fault      (core dumped) valgrind --leak-check=full --show-leak-kinds=all ./ex17 db.dat c 101 512

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.