This one should be quick, I think.
EDIT: This is for my CS113 class. I just needed to free all the memory. If Valgrind found any memory leaks, I'd lose points. :P
Regardless, I figured out that it apparently just required me to free stuff in Main that related to the return value of zero_pad. Once I did so, it worked fine. I'd mark this post as "complete" if I knew how.
char *zero_pad(struct cpu_t *cpu, char *string)
{
char *zero_string = malloc(cpu->word_size + 1);
int num_zeros = ((cpu->word_size) - strlen(string));
int i;
for(i = 0; i < num_zeros; i++)
{
zero_string[i] = '0';
}
return strncat(zero_string, string, strlen(string));
}
I need to free zero_string, since I allocated it. However, I have no idea how. If I free it before I return, then I've lost that data and can't return it. If I try to free it after, the function has already returned and thus can't go on to freeing it.
I tried to use strcpy to copy the string in zero_string into a new string, but I must have been doing it wrong, because I just ended up with a massive mess.
So, what do you all think?