Question: Having trouble with this error:Code 1 3 : Valgrind Detected Errors # + END _ SRC * * LINE DIFFERENCES from file 'test - results

Having trouble with this error:Code 13: Valgrind Detected Errors
#+END_SRC
** LINE DIFFERENCES
from file 'test-results/raw/prob1-10-diff-lines.tmp'
#+BEGIN_SRC text
If this section is empty, there are no line differences
ACTUAL: 53) Return Code 13: Valgrind Detected Errors
#+END_SRC
** VALGRIND LOG from: test-results/raw/prob1-10-valgrd.tmp
==11189== Memcheck, a memory error detector
==11189== Copyright (C)2002-2017, and GNU GPL'd, by Julian Seward et al.
==11189== Using Valgrind-3.17.0 and LibVEX; rerun with -h for copyright info
==11189== Command: ./test_bake_funcs12 bake_add_implicit_rules1
==11189== Parent PID: 11186
==11189==
==11189==
==11189== HEAP SUMMARY:
==11189== in use at exit: 8 bytes in 1 blocks
==11189== total heap usage: 8 allocs, 7 frees, 540,980 bytes allocated
==11189==
==11189==8 bytes in 1 blocks are definitely lost in loss record 1 of 1
==11189== at 0x4C2B067: malloc (vg_replace_malloc.c:380)
==11189== by 0x50C9B89: strdup (in /usr/lib64/libc-2.17.so)
==11189== by 0x403762: bake_add_implicit_rules (bake_funcs.c:97)
==11189== by 0x40222F: main (test_bake_funcs12.c:276)
==11189==
==11189== LEAK SUMMARY:
==11189== definitely lost: 8 bytes in 1 blocks
==11189== indirectly lost: 0 bytes in 0 blocks
==11189== possibly lost: 0 bytes in 0 blocks
==11189== still reachable: 0 bytes in 0 blocks
==11189== suppressed: 0 bytes in 0 blocks
==11189==
==11189== For lists of detected and suppressed errors, rerun with: -s
==11189== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Need to fix this code:
rule_t *bake_add_empty_rule(bake_t *bake){
// Check if the rule array needs to be resized
if (bake->rule_count == bake->rule_capacity){
// Double the capacity of the rule array
size_t new_capacity = bake->rule_capacity *2;
rule_t *new_rules = realloc(bake->rules, new_capacity * sizeof(rule_t));
if (new_rules == NULL){
perror("Error: memory reallocation failed");
return NULL;
}
// Update bake structure with the new rule array and capacity
bake->rules = new_rules;
bake->rule_capacity = new_capacity;
}
// Get a pointer to the next available rule slot
rule_t *new_rule = &(bake->rules[bake->rule_count]);
// Increment the rule count before potential reallocation
bake->rule_count++;
// Initialize the new rule with memset
memset(new_rule, 0, sizeof(rule_t));
// Return a pointer to the new empty rule
return new_rule;
}
//done
int bake_add_implicit_rules(bake_t *bake){
// Iterate over all rules appending implicit rules for any
// dependency that is not an explicit target.
for (int i =0; i < bake->rule_count; i++){
rule_t *rule = &(bake->rules[i]);
for (int j =0; rule->deps[j]!= NULL; j++){
char *dependency = rule->deps[j];
// Check if the dependency is an explicit target
int found =0;
for (int k =0; k < bake->rule_count; k++){
if (strcmp(bake->rules[k].target, dependency)==0){
found =1;
break;
}
}
// If the dependency is not an explicit target, add an implicit rule
if (!found){
// Allocate memory for the implicit rule target
char *implicit_target = strdup(dependency);
if (implicit_target == NULL){
perror("Error: memory allocation failed");
return -1;
}
// Add the implicit rule to the bake structure
rule_t *implicit_rule = bake_add_empty_rule(bake);
if (implicit_rule == NULL){
free(implicit_target); // Free memory in case of failure
return -1;
}
// Initialize the implicit rule
implicit_rule->target = implicit_target;
implicit_rule->deps[0]= NULL; // Mark the end of dependencies
SET_BIT(implicit_rule->rule_flags, RULE_IMPLICIT_BIT);
implicit_rule->cmd_count =0;
}
}
}
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!