Note that there are some explanatory texts on larger screens.

plurals
  1. POC: Help finding memory faults
    primarykey
    data
    text
    <p>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.</p> <p>The line I comment out (and where the program crashes) is </p> <pre><code>free(buffer); </code></pre> <p>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.</p> <pre><code>char* buffer = (char *) malloc(sizeof(char) * 30); </code></pre> <p>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.</p> <p>I don't think it is anything too complicated, I am just not familiar enough with C the recognize obvious memory mistakes.</p> <pre><code> // 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-&gt;blkidx = v; block_to_cache-&gt;content=(char*)malloc(BLOCK_SIZE); strcpy(block_to_cache-&gt;content,block_buffer); // ... } </code></pre> <p>In the test, BLOCK_SIZE is 5, QueueElement is a struct, content is a char* with BLOCK_BYTES of info.</p> <p>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...</p> <pre><code>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 </code></pre> <p>I think I get extra space because malloc allocates more space than I require, but I read that is normal.</p> <p>Any thoughts?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload