1

My code should print the numbers given as command line arguments. Instead it prints random-looking values.

What is going on and how can I fix it?

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>

void* kwadrat(void* arg) {
    int* x_ptr = *(int*)arg;
    int x = *x_ptrl;
    int wynik = x * x;
    printf("%d\n", wynik);
    return NULL;
}

int main(int argc, char* argv[]) {
    int value, n = argc - 1;
    pthread_t* thread = malloc(sizeof(pthread_t) * n);
    int* tab = malloc(sizeof(int) * n);
    for (int i = 0; i < n; i++) {
        tab[i] = atoi(argv[i + 1]);
        pthread_create(&thread[i], NULL, kwadrat, tab;
    }

    for (int i = 0; i < n; i++) {
        pthread_join(thread[i], NULL);
    }

    free(thread);
    free(tab);
    return 0;
}
New contributor
user31884282 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
7
  • 2
    Welcome to StackOverflow. Please take a tour and see How to Ask. Specifically: "it isnt working properly" - is too generel. Be specific about the current and expected behavior. Commented Nov 15 at 16:50
  • 1
    The random values were caused by dereferencing garbage memory due to the incorrect pointer handling - 1. Wrong argument passed to pthread_create (Line 18) - 2. Double dereference error (Line 6) - 3. Typo (Line 7) Commented Nov 15 at 16:52
  • 1
    "It is giving random values": This code should not have been able to compile at all, let alone run and output anything. The initialization of a pointer from an integer, misnamed variable, and missing parentheses should all be compiler errors. If you are using a online compiler or some other system that actually accepted this code and allowed it to run, throw it away. If you were submitting to some auto-grader or contest judging script, you should get in the habit of building and testing your code with a real compiler first. Commented Nov 16 at 17:53
  • 1
    It's kind of silly to turn to a generic chatbot for help before taking advantage of tools specifically designed for these tasks: compiler error and warning messages. Always enable compiler warnings, and address all of them before you even think about running the code. Commented Nov 16 at 17:57
  • 2
    And one final comment: you put "memory leak" in your title, despite that the problem has nothing to do with memory leaks, which is only going to mislead people. It's not clear why you thought the issue was a memory leak, but if you had some good reason for thinking that, you should have explained it in the text of the question. If you didn't, then please don't include wild guesses in your question (especially the title); it doesn't accomplish anything. Commented Nov 16 at 18:00

1 Answer 1

9

The random values were caused by dereferencing garbage memory due to the incorrect pointer handling -

  1. Wrong argument passed to pthread_create (Line 18)
  2. Double dereference error (Line 6)
  3. Typo (Line 7)

A fixed version:

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>

void* kwadrat(void* arg) {
    int* x_ptr = (int*)arg;      // Fixed: removed extra dereference
    int x = *x_ptr;              // Fixed: typo corrected
    int wynik = x * x;
    printf("%d\n", wynik);
    return NULL;
}

int main(int argc, char* argv[]) {
    int value, n = argc - 1;
    pthread_t* thread = malloc(sizeof(pthread_t) * n);
    int* tab = malloc(sizeof(int) * n);
    
    for (int i = 0; i < n; i++) {
        tab[i] = atoi(argv[i + 1]);
        pthread_create(&thread[i], NULL, kwadrat, &tab[i]);  // Fixed: &tab[i]
    }

    for (int i = 0; i < n; i++) {
        pthread_join(thread[i], NULL);
    }

    free(thread);
    free(tab);
    return 0;
}
New contributor
Gregory B is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Sign up to request clarification or add additional context in comments.

1 Comment

int* x_ptr = (int*)arg; should simply be int* x_ptr = arg;. Using needless casts just invites problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.