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

  • Home
  • SEARCH
  • 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 8610377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:06:56+00:00 2026-06-12T04:06:56+00:00

My program is written in C and it is a disk emulator. I finished

  • 0

My program is written in C and it is a disk emulator. I finished writing it and it runs when I comment out certain lines of my test program, but I get a memory error where I un-comment them. I suspect it is with my char* ‘s.

The line I comment out (and where the program crashes) is

free(buffer);

where buffer is the char* that the string of bytes that was read into from the disk. It was initially allocated 30 bytes using malloc.

char* buffer = (char *) malloc(sizeof(char) * 30);

There is too much to just post it all here, so I am going to put the parts where I am writing/copying to char* ‘s in the hopes that someone will see what I am doing wrong.

I don’t think it is anything too complicated, I am just not familiar enough with C the recognize obvious memory mistakes.

 // In the event of a cache miss:

 // block_buffer to pass to add_cache_entry
 char cMissBuffer[BLOCK_SIZE];

 // read content of block from disk 
 fread(cMissBuffer,sizeof(char),BLOCK_SIZE,diskEntity.pBlockStore);

 // add to cache
 if(1==add_cache_entry(i,cMissBuffer)) return 1;
 .
 .
 .
 // some of what is in add_cache_entry

 int add_cache_entry(int v, char *block_buffer)
 {
    // ...
    // construct a new queue element
    QueueElement *block_to_cache = (QueueElement*)malloc(sizeof(QueueElement));
    block_to_cache->blkidx = v;

    block_to_cache->content=(char*)malloc(BLOCK_SIZE);
    strcpy(block_to_cache->content,block_buffer);
// ...
 }

In the test, BLOCK_SIZE is 5, QueueElement is a struct, content is a char* with BLOCK_BYTES of info.

Here is an excerpt from running the executable (dumping the queue)…I think that the lack of a ‘\0’ could have something to do with the issue…

after adding cache entry (5):
DUMP:
BLOCK 5 FLAG:0 CONTENT:222220000000
BLOCK 4 FLAG:0 CONTENT:222220000000
BLOCK 3 FLAG:0 CONTENT:000000000000
BLOCK 2 FLAG:0 CONTENT:000000000000
BLOCK 1 FLAG:1 CONTENT:11100

I think I get extra space because malloc allocates more space than I require, but I read that is normal.

Any thoughts?

  • 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-12T04:06:57+00:00Added an answer on June 12, 2026 at 4:06 am

    A probable cause for the behaviour is that strcpy() requires the source string to be null terminated, which is not the case here as fread() does not append a null terminator for you (nor could it in this case as fread() is reading the exact buffer size). strcpy() also appends a null terminator which means the strcpy() call will definitely be writing beyond the block_to_cache->content buffer.

    If the data is not be used as a C style string use memcpy() to copy the data instead:

    memcpy(block_to_cache->content, block_buffer, BLOCK_SIZE);
    

    Other points:

    • Check the return value of fread(), to ensure it successfully populated the buffer before attempting to use it.
    • it is unnecessary to cast the return value of malloc() (see Do I cast the result of malloc?).
    • check return value of malloc() to ensure memory was successful allocated.
    • sizeof(char) is guaranteed to be one so can be removed from argument to malloc().
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Observe the following program written in Java (complete runnable version follows, but the important
I'v written some software that writes live video streams to disk. At a certain
I have written a program to process some data written to disk in big-endian
This is a test program that I have written for a larger project that
Suppose I have a program written in C/C++ and I'd like to find out
How can I get callbacks once data has been successfully written to disk in
I have program written in C. It takes 2 arguments username/password and try to
In the following program (written in irb shell) i call a method hidden of
That's my program written in C++ (OOP): #include <iostream> #include <string> using namespace std;
I have a membership program written with PHP and using mysql where users input

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.