1

Edit : the problem seems to be either mac os or valgrind, because even when I execute simple program like :

#include <stdio.h>

int main()
{
    int a;

    a = 28;
    printf("a = %d\n", a);
    return (0);
}

I keep having the Conditional jump error

I'm learning how to use threads in c, and I'm stuck with a 'Conditional jump or move depends on uninitialised value(s)' on valgrind when executing this code. I don't understand where the error commes from :

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
struct s_struct;
typedef struct s_test
{
    int q;
    int w;
    int e;
    struct s_struct *strct;
}               t_test;

typedef struct s_struct
{
    int a;
    int b;
    int c;
    t_test *test;
}               t_struct;

void *function(void *strct)
{
    t_struct *s;
    s = (t_struct *)strct;
    s->a = 42;
    s->test[2].q = 32;
    return (NULL);
}

int main()
{
    t_struct strct;
    pthread_t th;
    strct.test = (t_test *)malloc((sizeof(t_test) * 5));
    strct.a = 17;
    strct.test[2].q = 5;
    printf("s.a = %d\n", strct.a);
    printf("s.test[2].q = %d\n", strct.test[2].q);
    pthread_create(&th, NULL, &function, &strct);
    pthread_join(th, NULL);
    printf("s.a = %d\n", strct.a);
    printf("s.test[2].q = %d\n", strct.test[2].q);
    free(strct.test);
    return (0);
}

Could someone help me understand why I'm having this error.

The valgrind result is :

enter image description here

yuva
  • 93
  • 7
  • 1
    If you have text, then please copy-paste it as *text* into your question, don't show it as an image. Also please copy-paste (still as text) the full and complete error output you get, and tell us when and *where* (in your source code) you get it. – Some programmer dude Oct 09 '21 at 13:24
  • That what I did in the first place but stackoverlow keeps telling me that "It looks like your post is mostly code; please add some more details." – yuva Oct 09 '21 at 13:29
  • Which version of Valgrind? Of macOS? Can you repeat with a debug build ? That should tell you which printf is at the source of the error. Could you also try wiith `--track-origins=yes`? That might tell you where the memory came from. – Paul Floyd Oct 09 '21 at 13:32
  • What you mean by "repeat with a debug build" ? – yuva Oct 09 '21 at 13:44
  • I can't reproduce this. Should `function` return `void*` or just `void`? – Yun Oct 09 '21 at 13:54
  • 1
    @yuva A debug build is usually: no optimizations, and using the `-g` flag to include source code info in the executable. The latter allows Valgrind to, e.g., say at which exact source code lines the problems occur. – Yun Oct 09 '21 at 13:56
  • 1
    [Don't cast the result of `malloc`](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). And build with extra warnings enabled (e.g. `-Wall -Wextra`, at least), and treat all warnings as errors that must be fixed. – Some programmer dude Oct 09 '21 at 14:10
  • Ok, so I recompiled it with `-g -Wall -Wextra -Werror` and now I show the result of valgrind with the option `--track-origins=yes`. Apperently the error comes from the first printf at line 38, but still can't understand why. Maybe I have to initialise all variables of my structures even those there are not used ? – yuva Oct 09 '21 at 14:26
  • It's seems to be a false positif from valgrind, thank you guys anymay – yuva Oct 09 '21 at 14:43
  • If you explicitly included the header file needed for `malloc` (``) and don't cast the result of `malloc`, does that change anything? – Some programmer dude Oct 09 '21 at 18:27
  • Yes the result is still the same : `Conditional jump or move depends on uninitialised value(s)` – yuva Oct 09 '21 at 19:06
  • It could be a false positive. macOS is not well maintained in Valgrind at the moment. Do you need to build with -pthread on macOS? FWIW I get no errors on FreeBSD. – Paul Floyd Oct 10 '21 at 15:21
  • You could attempt to use the Clang sanitizers to see what they say. Read [the Clang documentation](https://clang.llvm.org/docs/index.html) for more information about the different sanitizers available and what they do. – Some programmer dude Oct 11 '21 at 06:20

0 Answers0