Valgrind swears at malloc ()
Language C, program:
#include <stdio.h> #include <stdlib.h> int main(int argc, char **argv) { long n, x, *args; scanf("%d", &n); args = (long*)malloc(n * sizeof(long)); if(!args) { printf("Allocation error\n"); return -1; } free(args); return 0; }
This code after entering a variable in valgrind gives the following error:
==27041== Conditional jump or move depends on uninitialised value(s) ==27041== at 0x4C2CE0C: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
Experiencedly found that the error appears only when I add the variable n to malloc. That is, for example, malloc (sizeof (long) * 5); it works without errors, but if the constant "5" is replaced with the variable n, valgrind starts swearing.
Why can this happen and what alternative is there?
ZY If you do not let the program through the debugger, then it works absolutely adequately.