Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8485999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:48:42+00:00 2026-06-10T20:48:42+00:00

I’m new to C and learning using Zed Shaw’s Learn C the Hard Way.

  • 0

I’m new to C and learning using Zed Shaw’s Learn C the Hard Way. I’m having some trouble finding out why I’m getting the following memory error. I’m pretty sure that I’m not leaking memory since I used Valgrind to check my code.

Here are the problematic lines of my code:

 :  int main(int argc,char *argv[]){

        if (argc<3) die ("USAGE : ex17 <dbfile> <action> [action params]");
       ...char *filename=argv[1];// how are we able to get away with one dimension?
        char action =argv[2][0];
        /*Line 166*/:struct Connection *conn=Database_open(filename,action);// error throwing line

The function Database_open is defined as :

/*Line 61*/:struct Connection *Database_open(const char *filename, char mode)
{
    /* data */
    struct Connection *conn= malloc(sizeof(struct Connection));

    if(!conn->db) die("Memory Error");//?

    if(mode=='c'){
        conn->file=fopen(filename,"w");
    }
    else{
        conn->file=fopen(filename,"r+");


    if(conn->file){
        Database_load(conn);
    }
    }


        if(!conn->file) die("Failed to open file");
        return conn;
    };

And here’s the die function that Im using :

void die (const char *message)
{
        if(errno){
            perror(message);
        }
        else{
            printf("ERROR :%s\n", message);
        }
        exit(1);
}

ANd when I run the code using Valgrind I get:

==6112== Command: ./ex17 db.dat c
==6112== 
==6112== Conditional jump or move depends on uninitialised value(s)
==6112==    at 0x80487B3: Database_open (ex17.c:61)//Marked above
==6112==    by 0x8048BAA: main (ex17.c:166)// Line numbers marked above
==6112== 
ERROR :Memory Error
==6112== 
==6112== HEAP SUMMARY:
==6112==     in use at exit: 8 bytes in 1 blocks
==6112==   total heap usage: 1 allocs, 0 frees, 8 bytes allocated
==6112== 
==6112== LEAK SUMMARY:
==6112==    definitely lost: 0 bytes in 0 blocks
==6112==    indirectly lost: 0 bytes in 0 blocks
==6112==      possibly lost: 0 bytes in 0 blocks
==6112==    still reachable: 8 bytes in 1 blocks
==6112==         suppressed: 0 bytes in 0 blocks
==6112== Rerun with --leak-check=full to see details of leaked memory
==6112== 
==6112== For counts of detected and suppressed errors, rerun with: -v
==6112== Use --track-origins=yes to see where uninitialised values come from
==6112== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

EDIT

Struct Connection

struct Connection
{
    /* data */
    FILE *file;
    struct Database *db;
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T20:48:44+00:00Added an answer on June 10, 2026 at 8:48 pm

    Thank you providing the details regarding the structure Connection. As is seen, the structure contains two pointer elements as members.

    After allocating the structure, you have to specifically malloc the element Database *db inside it.

    Something like:

    struct Connection *conn= malloc(sizeof(struct Connection));
    conn->db = malloc(sizeof(struct Database));
    

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
We're building an app, our first using Rails 3, and we're having to build
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.