Hi I am getting the the following error from valgrind.
Conditional move based on uninitialized values, Uninitalized values was created by heap allocation.
The compiler does not complain.
I looked at most of the similar errors at stackoverflow, but I can’t seem to pinpoint what’s wrong with mine.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int
main(void)
{
char *string1 = malloc(45);
char string2[25] = "HELLO WORLD";
strcpy(string1,string2);
printf("String one is %s\n",string1);
char string3[25];
for (int i = 0; i < 25; i++)
{
string3[i] = tolower(string1[i]);
}
printf("The output is %s\n",string3);
free(string1);
return 0;
}
You’re looping from 0 to 24 along
string1, which has only been initializedstrlen("HELLO WORLD") + 1bytes. Bytes indexed from 12 to 24 there have not been initialized, and you shouldn’t be trying to read them. You should either usecalloc()to allocatestring1or usememset()to initialize it, like so:or
Or you could initialize
string3to zeroes with the above methods and then only copystrlen(string1)bytes.